php - 通过 php 流式传输 mp3 文件

标签 php streaming

这是我通过 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-Dispositionattachment 更改为 inline

关于php - 通过 php 流式传输 mp3 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14952157/

相关文章:

video - H.264 实时流实际上是如何压缩和传输的?

youtube - 流式传输不同的音频和视频源

python - 使用Python进行Hadoop流传输:跟踪行号

php - 设置图像的宽度和高度

php - Yii 在浏览器选项卡关闭时更新数据库字段

php - file_get_contents => PHP fatal error : Allowed memory exhausted

php - 导出行值仅包含 NULL 值的 MySQL 表

javascript - JQuery - 点击/链接处理程序出现问题

android - 某些网址的广播流失败

C#捕获互联网音频流