HTML5音频 - currentTime属性不准确? [英] HTML5 audio - currentTime attribute inaccurate?

查看:975
本文介绍了HTML5音频 - currentTime属性不准确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在玩HTML5 < audio> 标签,我注意到一些奇怪的行为与 currentTime有关属性。

I'm playing around a bit with the HTML5 <audio> tag and I noticed some strange behaviour that has to do with the currentTime attribute.

我想播放本地音频文件并让 timeupdate 通过将 currentTime 属性与持续时间属性进行比较,事件检测完成。

I wanted to have a local audio file played and let the timeupdate event detect when it finishes by comparing the currentTime attribute to the duration attribute.

如果我让歌曲从开头到结尾播放,这实际上工作得很好 - 歌曲的结尾是正确的。

This actually works pretty fine if I let the song play from the beginning to the end - the end of the song is determined correctly.

但是,手动更改 currentTime (直接通过JavaScript或使用基于浏览器的音频控件)会导致API无法返回的正确值currentTime 了,但似乎比实际播放的位置提前了几秒。

However, changing the currentTime manually (either directly through JavaScript or by using the browser-based audio controls) results in the API not giving back the correct value of the currentTime anymore but seems to set it some seconds ahead of the position that's actually playing.

(这些几秒钟前面是基于Chrome的,Firefox似乎完全疯了,导致了差异越大。)

(These "some seconds" ahead are based on Chrome, Firefox seems to completely going crazy which results in the discrepancy being way bigger.)

关于这个问题的一个小问题: http://jsfiddle.net/yp3o8cyw/2/

A little jsFiddle example about the problem: http://jsfiddle.net/yp3o8cyw/2/

任何人都可以告诉我为什么会发生这种情况 - 或者我是不是说得对API应该怎么做?

Can anybody tell me why this happens - or did I just not getting right what the API should do?

PS:我刚注意到这实际上只发生在MP3编码的文件中,OGG文件完全正常。

P.S.: I just noticed this actually only happens with MP3-encoded files, OGG files are totally doing fine.

推荐答案

首先,我实际上无法在我的机器上重现您的问题,但我现在只有一个简短的MP3文件,所以可能是问题。无论如何,我想我可以解释发生了什么。

First off, I'm not actually able to reproduce your problem on my machine, but I only have a short MP3 file handy at the moment, so that might be the issue. In any case, I think I can explain what's going on.

MP3文件(MPEG)是非常简单的流,并且没有绝对的位置数据。读取文件的第一部分是不可能知道某个任意帧开始的字节偏移量。媒体播放器通过针头搜索在文件中寻找。也就是说,它知道整个轨道的大小,并且大致知道您的时间偏移距离轨道的距离。它猜测并开始解码,一旦与下一帧标题同步就立即拾取。这是一个不精确的过程。

MP3 files (MPEG) are very simple streams and do not have absolute positional data within them. It isn't possible from reading the first part of the file to know at what byte offset some arbitrary frame begins. The media player seeks in the file by needle dropping. That is, it knows the size of the entire track and roughly how far into the track your time offset is. It guesses and begins decoding, picking up as soon as it synchronizes to the next frame header. This is an imprecise process.

Ogg是一个更健壮的容器,并且在其框架头中内置了时间偏移。寻找一个Ogg文件要简单得多。

Ogg is a more robust container and has time offsets built into its frame headers. Seeking in an Ogg file is much more straightforward.

另一个问题是,大多数支持MP3的浏览器只是因为编解码器已在你的系统上可用。播放Ogg Vorbis和MP3通常是完全不同的库,具有不同的API。虽然Web标准在提供通用抽象方面做了很多工作,但是较小的实现细节会引起您所看到的怪癖。

The other issue is that most browsers that support MP3 do so only because the codec is already available on your system. Playing Ogg Vorbis and MP3 are usually completely different libraries with different APIs. While the web standards do a lot to provide a common abstraction, minor implementation details cause quirks like you are seeing.

这篇关于HTML5音频 - currentTime属性不准确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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