Analysis of Audio Autoplay Blocking on Browsers

举报
媒体服务小助手ultra 发表于 2024/08/21 11:01:09 2024/08/21
【摘要】 1.     What Is Audio Autoplay Blocking?                Automatically starting the playback of audio (or videos with audio tracks) immediately upon page load can be an unwelcome surprise to users. I...

1.     What Is Audio Autoplay Blocking?

                Automatically starting the playback of audio (or videos with audio tracks) immediately upon page load can be an unwelcome surprise to users. In order to give users control over this, browsers often block audio autoplay. Specifically, whenthe audio or video tag contains the autoplay attribute, autoplay feature would be blocked once page is loaded. If the play API is forcibly called, an error message similar to "Uncaught (in promise) DOMException: play() failed because the user didn't interact with the document first" will be reported.

HTML:

<video src="/video_with_audio.mp4" autoplay></video>

JavaScript:

videoElement.play();

When we using below function, autoplay of a video with an audio track on a new page will be stopped by the browser.

autoplay is not allowed by the browser until users engage with the page. For example, the play API will not be called until users click the playback button on the page.

PlayButton.addEventListener('click', () => {
  videoElement.play();
})

 

2.   Conditions for Audio Autoplay

In general, media will be allowed to autoplay only if at least one of the following conditions is met: 

  • The audio is muted or its volume is set to 0.
  • The user has engaged with the same-origin site (by clicking, tapping, pressing keys, etc.)
  • If the site has been whitelisted, autoplay may occur either automatically if the browser determines that the user engages with media frequently, or manually through preferences or other user interface features
  • Top frames need to delegate autoplay permission to cross-origin iframes to allow autoplay with sound. This permission is not required for same-origin iframes. Some browsers support this function, but the compatibility is limited.

a)     Muted autoplay:

If the muted attribute is added to the video tag, autoplay is allowed. Autoplay will be started once, you can click the button of unmuting audio.

HTML:

<video src="/video_with_audio.mp4" autoplay muted></video>

JavaScript:

videoElement.play();

b)    Same-origin permission grant:

Click the playback button on index.html to open the same-origin video-frame.html page. The video is automatically played with sound on the video-frame.html page.

index.html:

playButton.addEventListener('click', () => {
    window.open("video-frame.html","_blank");
})

 

1.png


video-frame.html:

document.querySelector('video').play()

2.1.png

If the video-frame.html page is refreshed, the autoplay will be blocked.

c)     Media Engagement Index (MEI)

The Media Engagement Index (MEI) measures an individual's propensity to consume media on a site. Chrome's approach is a ratio of visits to significant media playback events per origin:

  • Consumption of the media (audio/video) must be greater than seven seconds.
  • Audio must be present and unmuted.
  • The tab with the video is active.
  • Size of the video (in px) must be greater than 200 x 140.

From that, Chrome calculates a media engagement score. When the score is high enough, media autoplay is allowed on a device. The user's MEI can be found on about://media-engagement.

3.png


The exact situations that result in blocking, and the specifics of how sites become whitelisted, vary from browser to browser, but the above are good reference. 

You can set the browser startup parameter --user-data-dir=C:\Temp\chrome-data to point the browser data to a new directory and reset the MEI.

Reset MEI:

4.png 

d)    iframe delegation

The current page is embedded into the cross-origin video-frame.html page through the iframe:

var div = document.createElement("div");
div.innerHTML = '<iframe src="http://localhost:8080/video-frame.html" allow="autoplay"></iframe>';
document.body.appendChild(div);

Video autoplay on the cross-origin video-frame.html page:

<video src="/video_with_audio.mp4" autoplay></video>

5.png

After allow="autoplay" is added to the iframe tag, top frames delegate autoplay permission to iframes. Safari does not support this method. Please refer the below compatibility chart for more reference:

6.png

3.   Best Practices

a)     Muted autoplay:

 

Muted autoplay is allowed on a new page, and then users can click unmute to unmute the audio.

 

<video id="video" muted autoplay>
<button id="unmuteButton"></button>

<script>
  unmuteButton.addEventListener('click', function() {
    video.muted = false;
  });
</script>

7.png


b)    Listening on autoplay error:

If the error message NotAllowedError is reported after autoplay, the playback button can be displayed on the page to instruct users to click the playback button.

 

let startPlayPromise = document.querySelector('video').play()

if (startPlayPromise !== undefined) {
   startPlayPromise.then(() => {
      console.log('autoplay success')
    }).catch((error) => {
      if (error.name === "NotAllowedError") {
        console.log('autoplay error')
      } else {
        console.log('other error')
      }
    });
}

8.png

If the video tag is used to play only the video track and another audio tag is used to play the audio track, the video track is not restricted and can autoplay. Only the audio track is restricted. In this case, instruct users to click unmute.

Effect picture 1:

9.png

Effect picture 2:

10.png


Reference Documents:

mdn web docs- Autoplay guide for media and Web Audio APIs

Autoplay Policies in Chrome

Autoplay Policy Changes in macOS

Allowing or Denying Autoplay in Firefox


【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。