在HTML5音频播放器中寻求不一致 [英] Inconsistent seeking in HTML5 Audio player

查看:124
本文介绍了在HTML5音频播放器中寻求不一致的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从特定时间戳开始播放音频。但我甚至无法让最简单的例子正常工作。我尝试了以下操作,并修改了 w3school的示例

I want to play audio starting at a specific timestamp. But I can't even get the simplest example to work right. I tried the following, and also modifying w3school's example.

<body>
    <audio src="skyfall.mp3" id="audio" controls preload></audio>
    <button onclick="play()">Play</button>
</body>
<script type="text/javascript">
    var secs = 32;
    function play(){

        var p = document.getElementById("audio");
        p.currentTime = secs;
        console.log('Playing at secs: ' + secs); 
        p.play();
    }
</script>

但是每个浏览器都会播放不同的音频:Chrome for Windows大约延迟4秒,Chrome for Android似乎现场,移动Safari已关闭。 (甚至VLC在播放文件时也会遇到此问题。)如果从文件开头开始播放,它们将保持同步。

But different audio plays on each browser: Chrome for Windows is about 4 seconds late, Chrome for Android seems spot-on, Mobile Safari is off. (Even VLC has this issue when playing the file.) If playback starts from the beginning of the file, they stay in sync.

所以它看起来像HTML5音频标准要么执行不正确,要么说得不好。

So it looks to me like the HTML5 audio standard is either incorrectly implemented or poorly explained.

我读过服务器端的支持有时候会受到责备,但我不确定这是怎么回事当我在读本地文件时。最终,我希望在Cordova项目中实现这一目标。

I've read that server-side support is sometimes to blame, but I'm unsure how this would be an issue when I'm reading local files. Ultimately I want to get this working in a Cordova project.

任何想法?

推荐答案

问题在于文件编码。对于MP3文件,只有恒定比特率搜索在所有浏览器中正确/一致地工作。 W3表示MP3是所有浏览器正式普遍支持的唯一格式,因此我认为使用CBR MP3就是答案。也就是说,Mozilla有更多的深入的指南格式支持。

The issue is with file encoding. For MP3 files, only constant bit rate seeking works correctly/consistently across all browsers. W3 says that MP3 is the only format officially universally supported by all browsers, so I think using CBR MP3s is the answer. That said, Mozilla has a more in-depth guide on format support.

当比特率不恒定时,浏览器会在给定相同时间戳的情况下寻找不同的音频片段。寻找算法对于恒定比特率是简单的,但对于可变比特率更复杂(并且通常涉及某种形式的估计);我在HTML标准中找不到此操作的定义,因此不同的浏览器实现这一点并不令人惊讶。

When the bit rate is not constant, browsers seek to different segments of audio given the same timestamp. The algorithm to seek is simple for constant bit rate, but is more complex for variable bit rate (and often involves some form of estimation); I couldn't find a definition of this operation in the HTML standard, so it's unsurprising different browsers implement this differently.

这个答案有更多的讨论和潜在的解决方法。

This answer has some more discussion and potential workarounds.

这篇关于在HTML5音频播放器中寻求不一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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