通过php流mp3文件 [英] Streaming mp3 file through php

查看:78
本文介绍了通过php流mp3文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的php代码,用于通过php流mp3文件

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);

它可以在Mac Mini和所有其他PC上运行,但不能在iPad和iPhone上运行.甚至流媒体都可以在所有其他智能手机上使用.您的帮助将不胜感激.

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.

谢谢

推荐答案

<?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.

这篇关于通过php流mp3文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆