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

sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
John V
  • 875
  • 6
  • 12
  • 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 (http://stackoverflow.com/questions/11340276/make-mp3-seekable-php) – stepozer Sep 21 '15 at 06:42
  • Thanks. My problem solved – John V Sep 21 '15 at 06:58

1 Answers1

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);
1cgonza
  • 1,589
  • 10
  • 20