Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do you support seeking of an mp3 when returning from a php script?

I have an mp3 on my server (urls are just examples):

http://www.my-server.com/myaudio.mp3

I have a php script on the server at:

http://www.my-server.com/testmp3.php

Which contains the following code (which I got here):

<?
$file = "myaudio.mp3";

if (file_exists($file)) {
  header('Content-Description: File Transfer');
  header('Content-Type: application/octet-stream');
  header('Content-Disposition: attachment; filename='.basename($file));
  header('Content-Transfer-Encoding: binary');
  header('Expires: 0');
  header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  header('Pragma: public');
  header('Content-Length: ' . filesize($file));
  ob_clean();
  flush();
  readfile($file);
  exit;
}
?> 

Is this all I have to do to mimic the behavior so that both request behave the same way and return the exact same response? Or is there anything I'm missing.

I'm using some streaming code on iOS (not relevant here) and both requests stream the audio fine but I can't seek properly using the php request but I can with the mp3 request directly.

So without getting into details about the app itself I wanted to eliminate this one variable first. Is there anything I need to do to make sure that from another app's perspective these two request will return the exact same data?

Thanks for any input you can give me here.

Update

It turns out my question really should have read "how do you support seeking of an mp3 when returning from a php script?".

like image 254
nebs Avatar asked Oct 03 '11 17:10

nebs


2 Answers

To support seeking, you often will have to support a range request.

From the RFC: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.35

See also: Resumable downloads when using PHP to send the file?

like image 120
Brad Avatar answered Nov 19 '22 19:11

Brad


Its probably better to handle this with a .htaccess modification rather than some PHP code.
Here's a link on htaccess to get you started.

If you have a whole directory of .mp3 files that you want to appear as downloads instead of playing it in browser, you'd simply modify the .htaccess file in that folder to include

AddType application/octet-stream .mp3
like image 27
Mr. Llama Avatar answered Nov 19 '22 19:11

Mr. Llama