簡體   English   中英

我的 iframe 音頻不會在 Chrome 中自動播放,也不會隱藏在 Internet Explorer 中

[英]My iframe audio will not autoplay in Chrome and it will not hide in Internet Explorer

我正在嘗試將背景音樂添加到我正在編程的游戲中。 當我嘗試添加音樂然后在谷歌瀏覽器中打開程序時,播放欄被隱藏,但音樂不會自動播放。 當我在 Internet Explorer 中打開它時,音樂會自動播放,但播放欄是可見的。 我嘗試了很多東西( <audio><embed> ),但都沒有奏效。 這個當前的解決方案一半適用於 Chrome,一半適用於 Internet Explorer。

這是我的代碼:

<iframe src="TrollSongBurningMemory.mp3" allow="autoplay" id="audio" style="display: none !important;"></iframe>

試試這個,應該可以在 Chrome、IE、Safari 和 Firefox 中工作,但是,function 需要由用戶輸入初始化(即單擊按鈕)。 它不適用於頁面加載。 請注意,Chrome 自 2018 年 4 月起不允許媒體自動播放,除非用戶首先與網頁交互並滿足其他條件,詳情請點擊此處: https://developer.chrome.com/blog/autoplay/

<button onclick="playAudio();">Click me</button>
<audio id="foobar" src="TrollSongBurningMemory.mp3" preload="auto" style="display: none;">
<script>
function playAudio() {
var sample = document.getElementById("foobar");
sample.play();
}
</script>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM