如何preLOAD在Javascript声音? [英] How to preload a sound in Javascript?
问题描述
我可以preLOAD图像容易多亏了的onload
功能。但它不与音频工作。例如Chrome,Safari浏览器,Firefox等浏览器不支持的onload在音频标签
功能。
I can preload images easily thanks to the onload
function. But it doesn't work with audio. Browsers like Chrome, Safari, Firefox, etc. don't support the onload
functions in the audio tag.
我如何preLOAD在Javascript声音,而无需使用JS库,并且不使用或创建HTML标签?
How do I preload a sound in Javascript without using JS libraries and without using or creating HTML tags?
推荐答案
您的问题是音频对象不支持'负荷'事件。
Your problem is that Audio objects don't support the 'load' event.
相反,有一个名为canplaythrough,这并不意味着它是满载的事件,但也足够它被加载,在目前的下载速率,这将是由轨道有足够的时间,通过发挥的时间内完成。
Instead, there's an event called 'canplaythrough' that doesn't mean it's fully loaded, but enough of it is loaded that at the current download rate, it will finish by the time the track has had enough time to play through.
所以不是
audio.onload = isAppLoaded;
尝试
audio.oncanplaythrough = isAppLoaded;
或者更好的..;)
Or better yet.. ;)
audio.addEventListener('canplaythrough', isAppLoaded, false);
这篇关于如何preLOAD在Javascript声音?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!