I have built a PHP script that sends *.MP3 files form outside the public_html directory,
it works OK and sends the file. The problem is that I have a JavaScript script which should change the current time of the audio file (audioFile.currentTime = 25
;).
When I do this(Get the file from inside the public_html directory), it works:
// JavaScript:
var audioFile = new Audio("https://www.website.com/files/audioFile.mp3");
audioFile.play();
audioFile.currentTime = 15;
But, when I try to get the file from the following PHP script it sends the file OK. But, I can't change the "currentTime" using JavaScript
<?php
// This PHP file is public www.website.com/getAudioFile.php/
$fileLocation = "../audioFiles/audioFile.mp3";
if (file_exists($fileLocation))
{
header('Content-type: audio/mpeg');
header('Content-Disposition: inline; filename="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($file));
readfile($file);
}
?>
This is the JavaScript within which I tried to change the audio file currentTime:
<script>
var audioFile = new Audio("www.website.com/getAudioFile.php");
audioFile.play();
audioFile.currentTime = 15;
</script>
Each time I use object.currentTime = 15
, the audio files plays from the start.
If anyone knows what headers I should send or anything else I should do, please let me know how to solve my problem.
Shortly after I have posted this question, I found the solution to the problem.
In order to make the script work you will need to add the header('Accept-Ranges: bytes');
Like this:
<?php
// This PHP file is public www.website.com/getAudioFile.php/
$fileLocation = "../audioFiles/audioFile.mp3";
if (file_exists($fileLocation))
{
header('Content-type: audio/mpeg');
header('Content-Disposition: inline; filename="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
header('Content-Length: ' . filesize($file));
readfile($file);
}
?>