如何在 Javascript 中预加载声音?
借助 onload
函数,我可以轻松预加载图像。但它不适用于音频。 Chrome、Safari、Firefox 等浏览器不支持音频标签中的 onload
函数。
如何在不使用 JS 库且不使用或创建 HTML 标签的情况下在 Javascript 中预加载声音?
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.
How do I preload a sound in Javascript without using JS libraries and without using or creating HTML tags?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您的问题是音频对象不支持“加载”事件。
相反,有一个名为“canplaythrough”的事件,这并不意味着它已完全加载,而是已加载足够的内容,以当前的下载速率,它将在曲目有足够的时间播放时完成。
所以不要
尝试
或者更好..;)
Your problem is that Audio objects don't support the 'load' event.
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.
So instead of
try
Or better yet.. ;)
我尝试了 tylermwashburn 接受的答案,但它在 Chrome 中不起作用。所以我继续创建了这个,它受益于 jQuery。它还嗅探 ogg 和 mp3 支持。默认值为 ogg,因为一些专家表示 192KBS ogg 文件与 320KBS MP3 一样好,因此您可以节省 40% 的所需音频下载费用。然而 IE9 需要 mp3:
此外:
这是一个 mp3 到 ogg 转换器:
http://audio.online-convert.com/convert-to-ogg
或者您可以使用 VLC 媒体播放器进行转换。
右键单击 mp3 文件(在 Windows 中)并转到文件详细信息,检查您的 mp3 比特率。为新“ogg”文件选择新比特率时,尝试降低 40%。转换器可能会抛出错误,因此请继续增加大小,直到被接受。当然,测试声音以获得令人满意的质量。另外(这适用于我)如果您使用 VLC 媒体播放器来测试音轨,请确保将音量设置为 100% 或更低,否则您会听到音频质量下降的声音,并且可能会错误地认为这是压缩的结果。
I tried the accepted answer by tylermwashburn and it didn't work in Chrome. So I moved on and created this and it benefits from jQuery. It also sniffs for ogg and mp3 support. The default is ogg because some experts say a 192KBS ogg file is as good as a 320KBS MP3, so you save 40% on your required audio downloads. However mp3 is required for IE9:
Furthermore:
Here is an mp3 to ogg converter:
http://audio.online-convert.com/convert-to-ogg
Or you can use VLC Media player to convert.
Check your mp3 bitrate by right-clicking on the mp3 file (in Windows) and going to the file details. Try to reduce by 40% when selecting your new bitrate for your new 'ogg' file. The converter may throw an error, so keep increasing the size until is accepted. Of course test sounds for satisfactory quality. Also (and this applied to me) if you are using VLC Media player to test your audio tracks make sure you set the volume at 100% or below or otherwise you'll hear audio degradation and might mistakenly think it is the result of compression.
参考
Reference
根据您的目标浏览器,设置 audio 标记上的“WC3 HTML5 规范”>
prelaod
属性可能就足够了。Depending on your target browsers, setting the
prelaod
attribute on theaudio
tag may be sufficient.Remy 提出了一个利用 sprite 概念的 iOS 解决方案:
http://remysharp。 com/2010/12/23/audio-sprites/
不确定它是否直接解决预加载问题,但其优点是您只需要加载一个音频文件(我想这也是一个缺点)。
Remy came up with a solution for iOS that utilizes the sprite concept:
http://remysharp.com/2010/12/23/audio-sprites/
Not sure it directly addresses the preload, but has the advantage that you only need to load one audio file (which is also a drawback, I suppose).
您是否尝试对文件发出 ajax 请求?在它完全加载之前,您不会显示/使用它。
例如jQuery:如何预加载声音?(你不会不必使用 jQuery)。
Did you try making an ajax request for the file? You wouldn't show/use it until it was all the way loaded.
E.g. jQuery: How do you preload sound? (you wouldn't have to use jQuery).