HTML5 中的此音频有什么问题?
我对 HTML5 和 JavaScript 音频的东西感到非常愤怒。当它已经在 HTML 中时它工作得很好,但自从我将它转移到 JavaScript 后它就不起作用了。我不想使用 jQuery,因为我想保持尽可能小的大小。 所有函数都存在,变量也同样存在。 Chrome 没有给我任何错误,但网络下的 Chrome 开发者工具显示;待处理。所以我知道它获取了所有数据,但它只是不返回任何内容。它返回无负载和/或oncanplay。
请有人就我可能做错的事情或 Chrome 中可能存在的错误提出建议。
function loadAudio(loc,after){
window.itemsToLoad++;
var aud = new Audio(loc);
if(typeof(after)=="function"){
aud.addEventListener('load', function () {updateLoaders();alert('Yes');after(aud);}, false);
}else{
aud.addEventListener('load', function () {alert('Yes');updateLoaders();},false);
}
aud.preload="none";
aud.addEventListener('onerror', function () {alert('Error');updateErrorLoaders();},false);
aud.addEventListener('onabort', function () {alert('abort');updateErrorLoaders();},false);
aud.addEventListener('onemptied', function () {alert('empty');updateErrorLoaders();},false);
aud.type="type=\"audio/ogg\"";
aud.controls="controls";
aud.load();
return aud;
}
window.imageBackground = loadImage('images/background2.png',function (img) { alert("Yes it bloody works ;)");});
I've been getting very angry at this HTML5 and audio stuff with JavaScript. It worked fine when it was already in HTML but since I moved it to JavaScript its just not working. I do not wish to use jQuery because I want to keep the size as low as possible.
All the functions do exist and same goes for the variables. Chrome isn't giving me any errors but Chrome Developer tools under network says; pending. So I know it gets all the data but it just doesn't return anything. It returns no load and or oncanplay.
Please can someone advise me on something I maybe doing wrong or a possible bug in Chrome.
function loadAudio(loc,after){
window.itemsToLoad++;
var aud = new Audio(loc);
if(typeof(after)=="function"){
aud.addEventListener('load', function () {updateLoaders();alert('Yes');after(aud);}, false);
}else{
aud.addEventListener('load', function () {alert('Yes');updateLoaders();},false);
}
aud.preload="none";
aud.addEventListener('onerror', function () {alert('Error');updateErrorLoaders();},false);
aud.addEventListener('onabort', function () {alert('abort');updateErrorLoaders();},false);
aud.addEventListener('onemptied', function () {alert('empty');updateErrorLoaders();},false);
aud.type="type=\"audio/ogg\"";
aud.controls="controls";
aud.load();
return aud;
}
window.imageBackground = loadImage('images/background2.png',function (img) { alert("Yes it bloody works ;)");});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我注意到您的代码存在一些问题。首先,您定义一个
loadAudio
函数,但实际上是在调用loadImage('images/background2.png', fn)
。我认为这只是您复制并粘贴代码时的失误。其次,元素不支持
type
属性。如果您想指定媒体类型,请使用子元素。最后,您正在收听错误的事件。音频元素不响应
onload
事件,而是使用onloadeddata
事件。以下是您的代码,其中包含更新和一些扩展,以支持音频播放完毕时调用的回调:有关
元素支持的事件的更多详细信息,请参阅 W3Schools。
I've noticed a few problems with your code. First, you define a
loadAudio
function but you are actually callingloadImage('images/background2.png', fn)
. I assume this was just a slip up when you copy and pasted your code. Secondly, the<audio>
element doesn't support thetype
attribute. If you want to specify the media type then use a child<source>
element. Lastly, you are listening to the wrong event. Audio elements don't respond to theonload
event, instead use theonloadeddata
event. Here's your code with the updates and a little extension to support a callback that will be called when the audio has finished playing:For more details on the events supported by
<audio>
elements see W3Schools.