Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Streaming mp3 file through php

Tags:

php

streaming

Here is my php code to stream mp3 file through 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's working on Mac Mini and all other PCs but not working on iPad and iPhone. Even streaming is working on all other smart phones. Your help would be appreciated.

Thanks

like image 800
Anindya Avatar asked Feb 19 '13 07:02

Anindya


3 Answers

Why 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

like image 190
Mihai Iorga Avatar answered Nov 04 '22 04:11

Mihai Iorga


<?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 was from last year but someone might find this useful. This will stream the content.

like image 33
user3138558 Avatar answered Nov 04 '22 04:11

user3138558


I know this is old, but we just bumped into the same kind of problem with iOS.

Basically, it seems that if your app uses the native player to read the file, you NEED to implement Accept-Ranges and 206 Partial Content for your file to be read.

In our case, what was happening was that the file was 4 minutes long in total. The app would play for around 1min 50 seconds and then would loop back to the start. It would not detect the total length of the file.
Even though we had set up Accept-Ranges to none, iOS was ignoring it and was still asking for parts of the file. Since we were returning the whole thing, it was "looping" back to the beginning on the next "range" read.

For the implementation of Partial Content, we used https://mobiforge.com/design-development/content-delivery-mobile-devices, Appendix A: Streaming for Apple iPhone by Thomas Thomassen

I hope this helps someone

like image 1
ether Avatar answered Nov 04 '22 04:11

ether