我已经尝试使用控制面板在移动设备上使用<audio>
元素的各种本机监听器。但是,播放音频后关闭屏幕,音频不会暂停,但我看到m.youtube可以做到这一点。那么,当屏幕关闭时我怎么能暂停音频?
<audio controls loop id="a">
<!-- <source src="MerryChristmas.mp3" type="audio/mpeg"> -->
HTML5 audio not supported
</audio>
var audio = document.getElementById('a')
var source = document.createElement('source')
source.type='audio/mpeg'
source.src='./MerryChristmas.mp3'
audio.appendChild(source);
您可以尝试visibilitychange事件。我没有测试它,所以我不确定它是否会起作用,但DOCS似乎应该
document.addEventListener("visibilitychange", function() {
audio.pause();
});
编辑:从window.addEvent更改为document.addEvent ...