设置HTML5音频事件的粒度'timeupdate' [英] Setting the granularity of the HTML5 audio event 'timeupdate'

查看:187
本文介绍了设置HTML5音频事件的粒度'timeupdate'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用HTML5音频创建一个简单的循环功能,并且有一个非常原始的解决方案,如下所示:

  $ (audio).bind('timeupdate',function(){
if(audio.currentTime> = 26){
var blah = audio.currentTime;
audio.currentTime = 23;
console.log(blah);
}
})

这工作正常,但唯一的小故障是timeupdate事件不会非常一致地触发。例如,上面的console.log返回:
26.14031982421875



26.229642868041992

26.13462257385254



26.21796226501465

...等等。 (你的想法......不一致的时间)

显然,这对于时间很重要的应用程序(音乐应用程序)不起作用。所以我明显的解决方案是增加触发timeupdate事件的粒度。我还没有找到任何API文档...但是很想知道是否有办法做到这一点。

解决方案

我很抱歉,但这是它的工作方式。来自 html5规范


每15到250毫秒,或每当媒体控制器的媒体控制器位置改变时(无论发生的频率如何),用户代理必须排队任务以在MediaController上触发一个名为timeupdate的简单事件。


此外,


活动因此不会比大约66Hz或比4Hz慢(假设事件处理程序运行时间不超过250ms)。我们鼓励用户代理根据每次处理事件的系统负载和平均成本来改变事件的频率,使得UI更新不再比用户代理在解码视频时可以舒适地处理更频繁。 / p>

如果您仔细阅读规范,您可以了解 timeupdate 事件是一种尽力而为的事件。只要它不会影响性能,它就会启动。



您可以过滤丢弃某些事件的事件以平滑到达次,但恐怕不可能做相反的事。


I'm trying to create a simple looping feature using HTML5 audio and have a very primitive solution as follows:

$(audio).bind('timeupdate', function() {
  if (audio.currentTime >= 26){
    var blah = audio.currentTime; 
    audio.currentTime = 23;
    console.log(blah);
  }
})

This works fine but the only glitch here is that the timeupdate event doesnt trigger very consistently. For instance, the console.log above returned: 26.14031982421875

26.229642868041992

26.13462257385254

26.21796226501465

...etc. (You get the idea..inconsistent times)

Obviously this won't work for an application where the timing is important (music applications). So the obvious solution to me would be to increase the granularity with which the timeupdate event is triggered. I havent been able to find any API docs...but would love to know if there is a way to do this.

解决方案

I'm sorry, but that's the way it works. From the html5 specs:

Every 15 to 250ms, or whenever the MediaController's media controller position changes, whichever happens least often, the user agent must queue a task to fire a simple event named timeupdate at the MediaController.

Also,

The event thus is not to be fired faster than about 66Hz or slower than 4Hz (assuming the event handlers don't take longer than 250ms to run). User agents are encouraged to vary the frequency of the event based on the system load and the average cost of processing the event each time, so that the UI updates are not any more frequent than the user agent can comfortably handle while decoding the video.

If you read through the specification, you can get the idea that timeupdate event is something of a "best effort" kind of event. It will fire when it can and always as long as it does not to affect performance too much.

You could filter the events discarding some from time to time to smooth the arrival times, but I'm afraid it's not possible to do the opposite.

这篇关于设置HTML5音频事件的粒度'timeupdate'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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