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

标签 php html 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 从表中获取歌曲位置。尽管我允许范围,但代码当前正在等待整个文件加载,然后再播放甚至跳过轨道。有没有另一种方法来发送正确的 block 而不等待整个事情?

最佳答案

也许尝试手动输出文件 block ?

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/ .希望能帮助到你

关于php - 从 php 流式传输到 HTML5 音频标签并允许音频可搜索的简单方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47143802/

相关文章:

java - VLCJ 无法在客户端使用 http 打开音频流

php - 使用 PHP 删除文件 onclick

php - 如何使用联接根据在另一个表中找到的行从一个表中获取 mysql 行?

javascript - 试图覆盖 CSS...但它在 PHP iframe 中?

html - 为什么跨度类会移动?

iphone - 播放有声音的iPhone吗?

php - 获取内部数组键的数组值

javascript - 根据滚动动态调整div高度

html - Swift 解析 html 表

python - 解释从mp3文件对象中读取的unicode数据