Internet Explorer 10:<音频>“播放”事件处理程序仅发生一次

Internet Explorer 10: <audio> "play" Event Handler Occurs Only Once?

本文关键字:程序 一次 事件处理 播放 Explorer 音频 Internet      更新时间:2023-09-26

这是一个简单的测试(jsFiddle演示):

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Audio Test</title>
    </head>
    <body>
        <a href="javascript:document.getElementById('audio_test').play();">Play Audio</a>
        <audio id="audio_test" onplay="javascript:console.log('played');" onended="javascript:console.log('ended');">
            <source src="http://www.html5tutorial.info/media/vincent.ogg" type="audio/ogg">
            <source src="http://www.html5tutorial.info/media/vincent.mp3" type="audio/mpeg">
        </audio>
    </body>
</html>

使用上面的代码并多次播放音频文件,Internet Explorer 10 只运行一次console.log('played');。这种行为是Microsoft有意还是我做错了什么?有没有很好的解决方法?

当前规范说,触发play事件的前提是" paused 是新的假的"

http://www.w3.org/TR/html5/embedded-content-0.html#event-media-play

因此,这可能意味着它仅在第一次触发,并且从暂停状态切换后触发。您可以尝试拨打audioElement.pause() ended

onended="javascript:this.paused(); console.log('ended');

因此,虽然这似乎可以解决问题,但根据您的需要,它可能不是一个适用的解决方法。在这种情况下,也许您可以使用playing事件。