如何在 Javascript 中预加载声音? [英] How to preload a sound in Javascript?

查看:63
本文介绍了如何在 Javascript 中预加载声音?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

借助 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.

如何在不使用 JS 库和不使用或创建 HTML 标签的情况下在 Javascript 中预加载声音?

How do I preload a sound in Javascript without using JS libraries and without using or creating HTML tags?

推荐答案

您的问题是 Audio 对象不支持 'load' 事件.

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;

或者更好..;)

audio.addEventListener('canplaythrough', isAppLoaded, false);

这篇关于如何在 Javascript 中预加载声音?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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