从php流式传输到HTML5音频标签并允许音频可以搜索的简单方法?

时间:2017-11-06 18:53:58

标签: php html5 audio

我目前有这个

<audio id ="player">
    <source src="includes/playsong.php" id = "player">
</audio>

JS

var playsong = function (songName) {
    $('#playing').html(songName);
    if (currentSong === songName) {
        return;
    }
    $.post('includes/songrequest.php', {'request': songName}, function () {
        $('#source').attr("src", "includes/playsong.php/" + songName);
        player.load();
        play();
    });

$('#slider').slider({
    min: 0,
    max: 1,
    step: 0.001,
    change: function (event, ui) {
        player.currentTime = player.duration * ui.value;

        }
}

和PHP

$file = mysqli_fetch_assoc($song)['location'];
header('Content-Type: audio/' . pathinfo($file, PATHINFO_EXTENSION));
header('Content-Length: ' . filesize($file));
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
header('Cache-Control: no-cache');
readfile($file);

如果用户实际拥有使用该路径的歌曲,则php从表中获取歌曲位置。即使我允许范围,代码当前正在等待整个文件在播放之前加载甚至跳过曲目。是否有另一种方法可以在不等待整个事情的情况下发送正确的块?

1 个答案:

答案 0 :(得分:-1)

也许尝试手工输出文件?

set_time_limit(0);
$file = @fopen($file_path,"rb");
while(!feof($file)) {
    print(@fread($file, 1024*8));
    ob_flush();
    flush();
}

我在此处找到了此代码段http://www.media-division.com/the-right-way-to-handle-file-downloads-in-php/。希望它有所帮助