这是我通过 php 流式传输 mp3 文件的 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);
它适用于 Mac Mini 和所有其他 PC,但不适用于 iPad 和 iPhone。甚至流媒体也适用于所有其他智能手机。您的帮助将不胜感激。
谢谢
最佳答案
如果是歌曲,为什么要content-type: application/octet-stream
?更改标题:
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:删除了 Content-Transfer-Encoding
并将 Content-Disposition
从 attachment
更改为 inline
关于php - 通过 php 流式传输 mp3 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14952157/