用php播放音频

用php播放音频,php,audio,safari,Php,Audio,Safari,我正在使用我在互联网上找到的脚本(智能读取文件)使用php在页面上播放mp3: smartReadFile($filePath, $fileName, $mimeType); function smartReadFile($location, $filename, $mimeType = 'application/octet-stream') { if (!file_exists($location)) { header ("HTTP/1.1 404 Not Found");

我正在使用我在互联网上找到的脚本(智能读取文件)使用php在页面上播放mp3:

smartReadFile($filePath, $fileName, $mimeType);

function smartReadFile($location, $filename, $mimeType = 'application/octet-stream')
{
if (!file_exists($location))
{
    header ("HTTP/1.1 404 Not Found");
    return;
}

$size   = filesize($location);
$time   = date('r', filemtime($location));

$fm     = @fopen($location, 'rb');
if (!$fm)
{
    header ("HTTP/1.1 505 Internal server error");
    return;
}

$begin  = 0;
$end    = $size - 1;

if (isset($_SERVER['HTTP_RANGE']))
{
    if (preg_match('/bytes=\h*(\d+)-(\d*)[\D.*]?/i', $_SERVER['HTTP_RANGE'], $matches))
    {
        $begin  = intval($matches[1]);
        if (!empty($matches[2]))
        {
            $end    = intval($matches[2]);
        }
    }
}

if (isset($_SERVER['HTTP_RANGE']))
{
    header('HTTP/1.1 206 Partial Content');
}
else
{
    header('HTTP/1.1 200 OK');
}

header("Content-Type: $mimeType"); 
header('Cache-Control: public, must-revalidate, max-age=0');
header('Pragma: no-cache');  
header('Accept-Ranges: bytes');
header('Content-Length:' . (($end - $begin) + 1));
if (isset($_SERVER['HTTP_RANGE']))
{
    header("Content-Range: bytes $begin-$end/$size");
}
header("Content-Disposition: inline; filename=$filename");
header("Content-Transfer-Encoding: binary");
header("Last-Modified: $time");

$cur    = $begin;
fseek($fm, $begin, 0);

while(!feof($fm) && $cur <= $end && (connection_status() == 0))
{
    print fread($fm, min(1024 * 16, ($end - $cur) + 1));
    $cur += 1024 * 16;
}
}
smartReadFile($filePath,$fileName,$mimeType);
函数smartReadFile($location,$filename,$mimeType='application/octet stream')
{
如果(!file_存在($location))
{
标题(“未找到HTTP/1.1 404”);
返回;
}
$size=文件大小($location);
$time=date('r',filemtime($location));
$fm=@fopen($location,'rb');
如果(!$fm)
{
标题(“HTTP/1.1505内部服务器错误”);
返回;
}
$begin=0;
$end=$size-1;
如果(isset($\u服务器['HTTP\u范围]]))
{
if(preg_match('/bytes=\h*(\d+)-(\d*)[\d.*]?/i',$\u服务器['HTTP_RANGE',$matches))
{
$begin=intval($matches[1]);
如果(!empty($matches[2]))
{
$end=intval($matches[2]);
}
}
}
如果(isset($\u服务器['HTTP\u范围]]))
{
标题(“HTTP/1.1 206部分内容”);
}
其他的
{
标题(“HTTP/1.1200正常”);
}
标题(“内容类型:$mimeType”);
标头('Cache-Control:public,必须重新验证,最大期限=0');
标题('Pragma:no cache');
标题('Accept-Ranges:bytes');
标题('Content-Length:'。($end-$begin)+1));
如果(isset($\u服务器['HTTP\u范围]]))
{
标题(“内容范围:字节$begin-$end/$size”);
}
标题(“内容配置:内联;文件名=$filename”);
标题(“内容传输编码:二进制”);
标题(“上次修改:$time”);
$cur=$begin;
fseek($fm,$begin,0);

虽然(!feof($fm)&&$cur谢谢,但您的代码救了我!