HTML5 中的此音频有什么问题?

发布于 2024-11-08 07:01:09 字数 1221 浏览 3 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尤怨 2024-11-15 07:01:29

我注意到您的代码存在一些问题。首先,您定义一个 loadAudio 函数,但实际上是在调用 loadImage('images/background2.png', fn)。我认为这只是您复制并粘贴代码时的失误。其次, 元素不支持 type 属性。如果您想指定媒体类型,请使用子 元素。最后,您正在收听错误的事件。音频元素不响应 onload 事件,而是使用 onloadeddata 事件。以下是您的代码,其中包含更新和一些扩展,以支持音频播放完毕时调用的回调:

function updateLoaders() {
    // do stuff
}

function updateErrorLoaders() {
    // do stuff
}

function loadAudio(src, loadComplete, playComplete) {
    window.itemsToLoad++;
    var audio = new Audio();

    // Playback has completed.
    audio.addEventListener('ended', function () {
        if (typeof playComplete === 'function') {
            playComplete(audio);
        }
    }, false);
    // The entire song has loaded (not necessarily done playing).
    audio.addEventListener('loadeddata', function () {
        updateLoaders();

        if (typeof loadComplete === 'function') {
            loadComplete(audio);
        }
    }, false);

    // Handle errors.
    audio.addEventListener('error', function () {
        console.log('error');
        updateErrorLoaders();
    }, false);
    audio.addEventListener('abort', function () {
        console.log('abort');
        updateErrorLoaders();
    }, false);
    audio.addEventListener('emptied', function () {
        console.log('empty');
        updateErrorLoaders();
    }, false);

    audio.preload = 'none';
    audio.src = src;
    audio.controls = 'controls';
    audio.load();

    return audio;
}

var audio = loadAudio('Example.ogg', function (audio) {
    console.log('audio completely loaded');
    audio.play();
});

有关 元素支持的事件的更多详细信息,请参阅 W3Schools

I've noticed a few problems with your code. First, you define a loadAudio function but you are actually calling loadImage('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 the type 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 the onload event, instead use the onloadeddata 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:

function updateLoaders() {
    // do stuff
}

function updateErrorLoaders() {
    // do stuff
}

function loadAudio(src, loadComplete, playComplete) {
    window.itemsToLoad++;
    var audio = new Audio();

    // Playback has completed.
    audio.addEventListener('ended', function () {
        if (typeof playComplete === 'function') {
            playComplete(audio);
        }
    }, false);
    // The entire song has loaded (not necessarily done playing).
    audio.addEventListener('loadeddata', function () {
        updateLoaders();

        if (typeof loadComplete === 'function') {
            loadComplete(audio);
        }
    }, false);

    // Handle errors.
    audio.addEventListener('error', function () {
        console.log('error');
        updateErrorLoaders();
    }, false);
    audio.addEventListener('abort', function () {
        console.log('abort');
        updateErrorLoaders();
    }, false);
    audio.addEventListener('emptied', function () {
        console.log('empty');
        updateErrorLoaders();
    }, false);

    audio.preload = 'none';
    audio.src = src;
    audio.controls = 'controls';
    audio.load();

    return audio;
}

var audio = loadAudio('Example.ogg', function (audio) {
    console.log('audio completely loaded');
    audio.play();
});

For more details on the events supported by <audio> elements see W3Schools.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文