HTML5音频元素“src = get.php无法跳过位置

时间:2012-11-19 15:01:25

标签: php javascript html5 audio html5-audio

我正在使用HTML5 AudioElement播放我的.mp3文件,但我注意到无法跳过该位置。我是从www root外部通过PHP获取文件。

我正在使用HTML代码:

<audio src="" controls id="appAudio" style="width:1000px;margin-top:10px"></audio>

JavaScript代码:

document.getElementById('appAudio').src= 'http://localhost/index.php/player/get_audio_via_php/1';

PHP代码:

    $file = "/myfolder/1.mp3";
    header('Content-Type: audio/mpeg');
    header('Content-Length: ' . filesize($file));

    return file_get_contents($file);

就像我说它确实播放音频文件一样,但跳过位置是不可能的。 这个问题有方法解决吗?

谢谢

3 个答案:

答案 0 :(得分:4)

如果浏览器没有缓存中的文件,“跳过”会导致浏览器发送一个"range" header的新请求。您的PHP文件需要处理此标头。

这意味着:

  1. get and parse the range header
  2. respond to the request with status code 206 and corresponding range headers
  3. output only necessary bytes

答案 1 :(得分:3)

查看与serving mp3 files via php相关的帖子。

答案建议更改内容类型以包含几种类型:

header("Content-Transfer-Encoding: binary"); 
header("Content-Type: audio/mpeg, audio/x-mpeg, audio/x-mpeg-3, audio/mpeg3");

答案 2 :(得分:0)

老问题......我用过这个,似乎在使用Chrome。

    header("Content-Transfer-Encoding: binary"); 
    header("Content-Type: audio/mpeg, audio/x-mpeg, audio/x-mpeg-3, audio/mpeg3");
    header('Content-Disposition:: inline; filename="'.$audio->filename.'"');
    header('Content-length: '.$fileSize);
    header('Cache-Control: no-cache');
    header('Accept-Ranges: bytes');
    readfile($filepath);