3

I want to play audio which will be generated from a php script.

Client side:

<audio controls autoplay src="audio.php"></audio>

Server side:

$path = 'somefile.mp3';
header('Content-Type: audio/mpeg');
header('Cache-Control: no-cache');
header("Content-Transfer-Encoding: binary");
readfile($path);

It plays, but I can't change current the time in the audio element. The range slider is not working.

Result

2
  • 1
    I think the reason is you must make audio file seekable. For example when user rewind audio file this player try to get partial content (stackoverflow.com/questions/11340276/make-mp3-seekable-php) Commented Sep 21, 2015 at 6:42
  • Thanks. My problem solved Commented Sep 21, 2015 at 6:58

1 Answer 1

4

It is just missing a few things in the header in order for the seekbar to work. The Content-Length and Accept-Ranges. The HTML audio player needs them in order to build player seekbar.

Try this:

$path = 'somefile.mp3';
header('Content-Type: audio/mpeg');
header('Cache-Control: no-cache');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($path));
header('Accept-Ranges: bytes');
readfile($path);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.