How can I tell if an HTML5 Audio element is playing with Javascript

泄露秘密 提交于 2019-12-05 03:56:05
function isPlaying(playerId) {
    var player = document.getElementById(playerId);
    return !player.paused && !player.ended && 0 < player.currentTime;
}

I believe the browser stops the audio as part of the page unload process. Hence, by the time your custom unload function is called, the audio has been stopped.

You could use onbeforeunload instead of onunload - that might work (untested).

Alternatively, you could set a flag when the audio starts, and use the onended event to change that flag when the audio ends, or when the user manually stops or pauses it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!