smartReadFile icon indicating copy to clipboard operation
smartReadFile copied to clipboard

Stream Headers for Android

Open thepag opened this issue 11 years ago • 0 comments

Recording this information here, due to relevancy. The original post on jPlayer google group may have more information.

Dom Wyss Wrote:

Hi there,

Dave Rodriguez and me did spend a bit of time on this. For jPlayer to work with browsers on Android newer than 2.3, the stream must have different headers. From this and this stackoverflow answers, we came to this solution:

<?php
$path       = __DIR__ . 'tracks/track.mp3';
$fileSize   = filesize($path); 
$strContext = stream_context_create(
   array(
       'http'=>array(
       'method'=>'GET',
       'header'=>"Accept-language: en\r\n"
       )
   )
);

header('Accept-Ranges:    none');
header('Cache-Control:    no-cache');
header('Content-type: audio/mpeg');
header('Pragma:    no-cache');

$userAgent = $_SERVER['HTTP_USER_AGENT'];

if (is_int(strpos($userAgent, 'Android')) && !is_int(strpos($userAgent, 'Android 2')))
{ 
    header('Transfer-Encoding:    none');
    header('Connection:    close');
}
else
{
  header('Content-Length:    ' . $fileSize);
}

$fpOrigin = fopen($path, 'rb', false, $strContext);
while(!feof($fpOrigin)){
    $buffer = fread($fpOrigin, 4096);
    echo $buffer;
    flush();
}
fclose($fpOrigin);
exit;

Hope this helps somebody!

All the best Dom

thepag avatar Nov 21 '14 15:11 thepag