Pass mp3 player event time to a php variable

I am using an mp3 audio player in a php script as follows

   echo "<b><font color='red'>$pic1</b></font>";
}
?>
<br><br><audio controls preload='none'><source src="<?php echo $namehref;?>" type='audio/mp3' /></audio>
<?php
echo "<b><font color='blue'> ";
?>

I want to capture the playback value to a php variable so that I can save the variable embedded in a line with other variables
$click = $Date. " ". $namehref. " ". $country. " ". $city. " ". $region. " ". $playback_value;

and then send it to to a text file

You would first need to capture it in the browser using appropriate JavaScript methods.
Php is not available while the player is playing, because php runs on server and mp3 player on client.

The captured value must be send to the server though to allow it to be processed by some PHP script.
You can use the Javascript fetch API to send data to the server without reloading the page

This code gives me what I want from the button ‘get current time position’ could you give me a sample code to pass the playback time to the php playback_value

<!DOCTYPE html> 
<html> 
<body> 

<button onclick="getCurTime()" type="button">Get current time position</button>
<button onclick="setCurTime()" type="button">Set time position to 5 seconds</button><br> 

<video id="myVideo" width="320" height="176" controls>
  <source src="https://www.w3schools.com/TAgs/mov_bbb.mp4" type="video/mp4">
  Your browser does not support HTML5 video.
</video>

<script>
let vid = document.getElementById("myVideo");

function getCurTime() { 
  alert(vid.currentTime);
} 

function setCurTime() { 
  vid.currentTime=5;
} 
</script> 

<p>Video courtesy of <a href="https://www.bigbuckbunny.org/" target="_blank">Big Buck Bunny</a>.</p>

</body> 
</html>

function getCurTime() { 
  let script_url = "https://example.com/myphpscript.php?curtime=" + vid.currentTime;


try {
    const response = await fetch(script_url);
    if (!response.ok) {
      throw new Error(`Response status: ${response.status}`);
    }

    const answer_text = await response.text();
    console.log("php script result: " + answer_text);
  } catch (error) {
    console.error(error.message);
  }
} 


In your processing php script ( in the example myscript.php )
you can then access the value from $_GET[‘curtime’] and save it wherever you need to save it.
Whatever you then echo in the php script will afterwards be included in the Javascript answer_text and logged to console.

Please be aware that these are just minimal examples and in your php you must validate the content of curtime appropriately before using it, because technically everyone can send you everything including bad things.

Furthermore you may want to include additional checks to make sure that the call is indeed coming from your player and not elsewhere.

Sponsor our Newsletter | Privacy Policy | Terms of Service