JavaScript-Chrome打开新窗口,自动播放音频

时间:2018-07-25 08:12:10

标签: javascript html5 google-chrome html5-audio

当我使用JavaScript打开流音频窗口时,它拒绝在Google Chrome浏览器(版本67.0.3396.99)上自动播放。

我已在元素上设置了“自动播放”属性

<audio id="stream" controls autoplay>

我什至试图在页面中使用嵌入式JavaScript来

document.getElementById("stream").play();

问题是Chrome阻止了任何play()互动,因为

  

未捕获(承诺)的DOMException:play()失败,因为用户没有首先与文档进行交互。

有人对我有建议的解决方案吗?

编辑:在元素上设置静音无济于事,因为它仍然需要额外的单击才能取消音频静音。

谢谢

:-)

1 个答案:

答案 0 :(得分:0)

这是一个添加了道具muted的代码片段,并且没有显示任何错误。检查并让我知道,这里是小提琴http://jsfiddle.net/Aravi/9ng3tszq/

window.addEventListener("load",function(){
 var player1 = document.getElementById("player");
 player1.play();
})
<audio id="player" controls autoplay muted>
   <source src="http://d2cstorage-a.akamaihd.net/wbr/gotnext/8578.mp3" type="audio/mp3">
</audio>