javascript– 当进程为Backgrounded时,如何在Android Chrome中禁用音频播放?

javascript– 当进程为Backgrounded时,如何在Android Chrome中禁用音频播放?,第1张

概述如何以编程方式禁用在ChromeAndroid进程后台播放的歌曲?以下是在Chrome中播放歌曲的页面的简单示例:https:/homashunter.name/examples/chrome-audio-bug.htmlvarsong=newAudio('song.ogg');song.loop='loop';button=document.getElementById('play');button.ad

如何以编程方式禁用在Chrome Android进程后台播放的歌曲?

以下是在Chrome中播放歌曲的页面的简单示例:

https://thomashunter.name/examples/chrome-audio-bug.html

var song = new Audio('song.ogg');song.loop = 'loop';button = document.getElementByID('play');button.addEventListener("click", function() {  song.play();});

注意这首歌将如何在后台继续播放.虽然很适合自动点唱机,但它会让网络游戏玩家疯狂.

有没有办法禁用Chrome中单个音频元素的后台播放?或者,至少有一个回调,当页面失去焦点,所以我可以运行song.stop()?

解决方法:

您可以尝试使用visibilitychange:

document.addEventListener('visibilitychange', function(){    if (document.hIDden) {        // document is hIDden         // This re-initialize the audio element        // to release the audio focus        song.load();     }    else {        // document is focused    }},false);

这里有完整的例子https://jsfiddle.net/6001wsf9/

在AndroID 5.0上使用Chrome 43.0.2357.78进行了测试

总结

以上是内存溢出为你收集整理的javascript – 当进程为Backgrounded时,如何在Android Chrome中禁用音频播放?全部内容,希望文章能够帮你解决javascript – 当进程为Backgrounded时,如何在Android Chrome中禁用音频播放?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1119191.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-29
下一篇2022-05-29

发表评论

登录后才能评论

评论列表(0条)

    保存