在移动浏览器上关闭屏幕时无法暂停音频

问题描述 投票:0回答:1

我已经尝试使用控制面板在移动设备上使用<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);
javascript android html ios audio
1个回答
0
投票

您可以尝试visibilitychange事件。我没有测试它,所以我不确定它是否会起作用,但DOCS似乎应该

document.addEventListener("visibilitychange", function() {
  audio.pause();
});

编辑:从window.addEvent更改为document.addEvent ...

© www.soinside.com 2019 - 2024. All rights reserved.