Streaming mp3 file via php

Here is my php code for streaming mp3 file via php

set_time_limit(0); $dirPath = "path_of_the_directory"; $songCode = $_REQUEST['c']; $filePath = $dirPath . "/" . $songCode . ".mp3"; $strContext=stream_context_create( array( 'http'=>array( 'method'=>'GET', 'header'=>"Accept-language: en\r\n" ) ) ); $fpOrigin=fopen($filePath, 'rb', false, $strContext); header('content-type: application/octet-stream'); while(!feof($fpOrigin)){ $buffer=fread($fpOrigin, 4096); echo $buffer; flush(); } fclose($fpOrigin); 

It works on Mac Mini and other PCs, but does not work on iPad and iPhone. Even streaming works on all other smartphones. Your help will be appreciated.

thanks

+7
source share
3 answers

Why is content-type: application/octet-stream if it's a song? Change the headers:

 set_time_limit(0); $dirPath = "path_of_the_directory"; $songCode = $_REQUEST['c']; $filePath = $dirPath . "/" . $songCode . ".mp3"; $strContext=stream_context_create( array( 'http'=>array( 'method'=>'GET', 'header'=>"Accept-language: en\r\n" ) ) ); $fpOrigin=fopen($filePath, 'rb', false, $strContext); header('Content-Disposition: inline; filename="song.mp3"'); header('Pragma: no-cache'); header('Content-type: audio/mpeg'); header('Content-Length: '.filesize($filePath)); while(!feof($fpOrigin)){ $buffer=fread($fpOrigin, 4096); echo $buffer; flush(); } fclose($fpOrigin); 

LE: removed Content-Transfer-Encoding and changed Content-Disposition from attachment to inline

+3
source
 <?php set_time_limit(0); $dirPath = "path_of_the_directory"; $songCode = $_REQUEST['c']; $filePath = $dirPath . "/" . $songCode . ".mp3"; $bitrate = 128; $strContext=stream_context_create( array( 'http'=>array( 'method'=>'GET', 'header'=>"Accept-language: en\r\n" ) ) ); header('Content-type: audio/mpeg'); header ("Content-Transfer-Encoding: binary"); header ("Pragma: no-cache"); header ("icy-br: " . $bitrate); $fpOrigin=fopen($filePath, 'rb', false, $strContext); while(!feof($fpOrigin)){ $buffer=fread($fpOrigin, 4096); echo $buffer; flush(); } fclose($fpOrigin); 

I know this post has been from last year, but someone might find this helpful. This will result in a flow of content.

+3
source

I know this is outdated, but we just ran into the same issue in iOS.

Basically, it seems that if your application uses its own player to read a file, you need to implement Accept-Ranges and 206 Partial Content for your file. to read.

In our case, it happened that the file was only 4 minutes. The application will play for about 1 minute 50 seconds, and then return to the beginning. It will not determine the total file length.
Although we set Accept-Ranges to nothing, iOS ignored it and still requested parts of the file. As we brought it all back, it was looping back to the beginning of the next reading of the range.

To implement partial content, we used https://mobiforge.com/design-development/content-delivery-mobile-devices , Appendix A: Streaming for Apple iPhone by Thomas Tomassen

I hope this helps someone

+1
source

All Articles