天天看點

audio.1.html:39 Uncaught (in promise) DOMException: play() failed because the user didn't interact with the document first.

一、audio.1.html:39 Uncaught (in promise) DOMException: play() failed because the user didn't interact with the document first.

Audio 标簽的 autoplay 自動播放功能受限制。

針對于新版谷歌浏覽器、火狐浏覽器。

對于IE,Edge浏覽器還是支援自動播放的。

規則要求:

在使用者發生互動的情況下,開啟播放。禁止頁面加載完,自動播放。

Chrome報錯提示最為友善,意思是說,使用者還沒有互動,不能調play。使用者的互動包括哪些呢?包括使用者觸發的touchend, click, doubleclick或者是 keydown事件,在這些事件裡面就能調play.

二、Chrome的autoplay政策在2018年4月做了更改。

新的行為:浏覽器為了提高使用者體驗,減少資料消耗,現在都在遵循autoplay政策,Chrome的autoplay 政策非常簡單

muted autoplay始終被允許

音樂的autoplay 隻有在下面集中情況下起作用:

有使用者行為發生像(click,tap,etc).

對于桌面程式,使用者已經提前播放了音頻

對于移動端使用者将音頻網址home screen.

1、 打開chrome

2、輸入 chrome://flags/#autoplay-policy

3、點選default,選擇 Setting No user gesture is required

4、 重新開機chrome

三、Audio Context 

The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page.

使用Audio Context 解決自動播放問題: 新版谷歌浏覽器不支援自動播放,火狐 或者Edge浏覽器支援

繼續閱讀