使用自动播放使用 HTML5 音频播放电台
Use autoplay to play a radio station using HTML5 audio
我正在尝试在 html 的文件上自动启动网络广播。目前使用自动播放失败,我假设收音机错过了加载一些数据的时间,因此自动播放被阻止了。
我创建了一个 setTimeout 函数来处理它。现在该功能拒绝在 Firefox 和 Opera 上启动。 Firefox 的错误日志信息量最大,转载于此:
NotAllowedError: The play method is not allowed by the user agent or
the platform in the current context, possibly because the user denied
permission
这是我的 html 的片段:
<body>
<audio controls src="http://5.63.151.52:7136/stream?icy=http" autoplay id="myAudio">
Your browser does not support the
<code>audio</code> element.
</audio>
<button onclick={goPlay()}>
Play
</button>
<script>
// function goPlay(){
// console.log("in goplay")
setTimeout(() => {
var x = document.getElementById("myAudio");
// function playAudio() {
x.play();
},
1000
)
// }
</script>
</body>
如何处理这种情况?
任何提示都很好,
谢谢
Firefox 错误准确地告诉您发生了什么。自动播放不起作用,因为用户(或更具体地说,浏览器 - 参见 here)阻止了它。
Any playback that happens before the user has interacted with a page
via a mouse click, printable key press, or touch event, is deemed to
be autoplay and will be blocked if it is potentially audible.
就我而言,当我尝试在浏览器控制台中执行 document.getElementsByTagName("video")[0].play()
时,在您提到的错误消息上方,有一条警告 Autoplay is only allowed when approved by the user, the site is activated by the user, or media is muted.
。我能够通过先将视频静音或在 Firefox 中为网站权限选择 "Allow Audio and Video" 来执行代码。
我正在尝试在 html 的文件上自动启动网络广播。目前使用自动播放失败,我假设收音机错过了加载一些数据的时间,因此自动播放被阻止了。 我创建了一个 setTimeout 函数来处理它。现在该功能拒绝在 Firefox 和 Opera 上启动。 Firefox 的错误日志信息量最大,转载于此:
NotAllowedError: The play method is not allowed by the user agent or the platform in the current context, possibly because the user denied permission
这是我的 html 的片段:
<body>
<audio controls src="http://5.63.151.52:7136/stream?icy=http" autoplay id="myAudio">
Your browser does not support the
<code>audio</code> element.
</audio>
<button onclick={goPlay()}>
Play
</button>
<script>
// function goPlay(){
// console.log("in goplay")
setTimeout(() => {
var x = document.getElementById("myAudio");
// function playAudio() {
x.play();
},
1000
)
// }
</script>
</body>
如何处理这种情况?
任何提示都很好, 谢谢
Firefox 错误准确地告诉您发生了什么。自动播放不起作用,因为用户(或更具体地说,浏览器 - 参见 here)阻止了它。
Any playback that happens before the user has interacted with a page via a mouse click, printable key press, or touch event, is deemed to be autoplay and will be blocked if it is potentially audible.
就我而言,当我尝试在浏览器控制台中执行 document.getElementsByTagName("video")[0].play()
时,在您提到的错误消息上方,有一条警告 Autoplay is only allowed when approved by the user, the site is activated by the user, or media is muted.
。我能够通过先将视频静音或在 Firefox 中为网站权限选择 "Allow Audio and Video" 来执行代码。