HTML5 音频标签 / mediaelement.js 的加载指示器
javascript/jQuery 有没有办法知道 标签的源文件是否已经/正在加载?
我正在使用 mediaelement.js,但是我会接受仅适用于常规 标记的答案。目前我正在伪造加载指示:(
$("#temp-loading").spin(minySpinner);
setTimeout(function() {
$('#temp-loading').spin(false);
}, 12000);
spin()
是来自 的函数spin.js)。显然,该指示器在这种情况下毫无意义,但是当页面加载但音频需要大约 15 秒才能开始播放时,人们会感到困惑,所以我不得不实施这个临时解决方案。不过,我希望它能够实际指示文件是否仍在加载,因此在较慢的连接上,图标将保留,直到文件实际开始播放。
另外值得注意的是,我将元素设置为 autoplay
(没关系,我保证!),因此当页面加载时,它会自动开始“播放”,即使尚未发生实际的音频。这使得很难使用 play
事件来停止旋转器,因为它会立即停止。因此,我要么必须在音频实际开始时停止旋转器,要么在音频实际准备好播放之前不播放(如果这是有意义的)。
我还注意到,在加载音频文件时,时间指示器显示“00:00”。加载后,它会更改为“00:00:00”。所以看起来,做我需要的事情的能力已经是内置的,我只需要知道如何使用它。
如果有区别,则该文件是实时流(通过 Icecast)。谢谢!
Is there a way for javascript/jQuery to know if the source file for an <audio>
tag has been/is being loaded?
I am using mediaelement.js, however I'll accept an answer that works for just a regular <audio>
tag. Currently I'm faking the loading indication:
$("#temp-loading").spin(minySpinner);
setTimeout(function() {
$('#temp-loading').spin(false);
}, 12000);
(spin()
is a function from spin.js). Obviously the indicator is meaningless in this case, but people were getting confused when the page was loaded but the audio was taking about 15 seconds to start playing, so I had to implement this temporary solution. I'd like it to actually indicate if the file is still being loaded, though, so on slower connections the icon will remain until the file has actually started playing.
Also of note is that I have the element set to autoplay
(it's okay, I promise!), so when the page is loaded it automatically starts "playing", even though no actual audio is happening yet. This makes it hard to use the play
event to stop the spinner, since it just stops immediately. So I'll either have to stop the spinner when the audio actually starts, or not PLAY until the audio is actually ready to be played, if that makes sense.
I also noticed that while the audio file is loading, the time indicator says, "00:00". When it's loaded, it changes to "00:00:00". So it seems that the ability to do what I need is already built-in, I just need to know how to use it.
If it makes a difference, the file is a live stream (via Icecast). Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
canplay
事件怎么样?编辑
既然我得到了要点,我最好把砖头的解决方案贴给后人:
What about the
canplay
event?EDIT
Since I got the points, I'd better paste brick's solution for posterity:
我已经通过以下方法解决了一半:
但这不是跨浏览器解决方案(适用于 Chrome 和 Safari,不适用于 Firefox)。
编辑 我现在可以使用 jQuery,但仍然不适用于 Firefox(它使用 mediaelement 的 Flash 后备)。此时它不再是一个 html5 问题,而是一个 mediaelement 问题。
编辑 2 我最终按照 Tetaxa 的建议让它工作......不完全是他/她的建议,但它给了我使用
success
回调的想法:谢谢!
I've half-way solved it with the following:
However this is not a cross-browser solution (works in Chrome and Safari, not Firefox).
edit I have it working with jQuery now, but still doesn't work for Firefox (which is using mediaelement's Flash fallback). At this point it's not an html5 question anymore, it's a mediaelement problem.
edit 2 I ended up getting it work following Tetaxa's suggestion... not exactly what he/she suggested, but it gave me the idea to use the
success
callback:Thanks!