Raj*_*jat 6 javascript audio html5
我正在使用HTML5音频.对于我的用例,我需要监听播放的音频持续时间,一旦超过某个阈值,就暂停音频.所以类似于:
$(audio).bind('timeupdate', function() {
if (audio.currentTime >= 10){
audio.pause();
}
});
Run Code Online (Sandbox Code Playgroud)
我所注意到的是,我的处理程序执行的时间,audio.currentTime大约是10.12878,10.34023等等,因此,它被暂停之前的一些点点额外的音频播放.
另一个问题似乎记录了同样的问题.问题是在2012年,所以我想知道现有技术是否有所改进.
如果没有,还有哪些其他方法可以更精确地完成这项工作?我之前没有使用音频,我真的很感激帮助.
So I researched my way through this problem and came out with the following findings:
1) HTML Audio is no dice for such a scenario. Its a very limited API and doesn't offer fine grain audio control. Web Audio API is the way to go for this.
2) Web Audio API is not the easiest thing to read/understand and is perhaps weakly implemented across various browsers. Its better to find a nice wrapper around it that fallsback to web audio or flash when Web Audio is not available in a browser.
3) Some wrappers exist like howler.js, sound.js and SoundManager2. Off the three SoundManager2 seems the best fit at first thought.
Demo 4G at http://www.schillmania.com/projects/soundmanager2/demo/api/ effectively solves a very similar problem as in the scenario in the question.
Update: SoundManager2 actually doesn't support the web audio api. From the technotes:
SM2 does not presently use the Webkit Audio API. It may be experimentally or more formally added at some point in the future, when the API is more universally-supported.