In order to improve the user experience, the browser has strengthened the restrictions on the autoplay policy (Autoplay): The browser does not allow the automatic playback of media with sound before there is no interactive operation.
Each browser has different implementations of the autoplay policy. For details, please refer to the following article:
When using the ZEGO Web SDK to pull the stream, the media stream containing audio and not muted will be restricted by the browser's auto-play policy, resulting in the audio not playing normally.
In actual use, the page is not totally restricted by Autoplay. As the number of times users use this page increases, the browser will add this page to its Autoplay whitelist. According to this principle, the developer can guide the user to click a certain position on the page to resume playback when it detects that the playback fails.
// myVideo: The developer's own rendering tag, the tag adds the autoplay attribute, and there is no muted attribute or muted=true
let autoPlay = false;
myVideo.oncanplay = ()=>{
setTimeOut(()=>{
autoPlay = !myVideo.paused
},1000)
}
Local streams generally do not have Autoplay restrictions (because no sound will be played), only remote streams are required.
You can directly bypass Autoplay restrictions through the following two solutions:
Solution | Description | Applicable Scenarios |
---|---|---|
Solution 1 | Set the "muted" attribute of the <video> or <audio> tag to true before playing. If the media does not contain sound, it will not be restricted by Autoplay. | Product design requires media to be played automatically without user operation. |
Solution 2 | Use UI and product design to allow users to interact with the page (click or touch), and then call the "play" method of the <video> or <audio> tag to play. | The product design allows the user to interact with the page before the media is played. For example, the user needs to click certain buttons after entering the live broadcast room to start subscribing to the anchor. |
No matter which solution is used, under the restrictions of the automatic playback policy, it is impossible to automatically play audio media without user operation. Although the browser maintains a whitelist locally to determine which websites to remove the autoplay restriction, the whitelist cannot be detected by Javascript.
Set the "muted" attribute of the <video> or <audio> tag to true, and the media will be automatically played by muting first, and then automatically cut to audio media playback when there is any interactive operation on the page.
Register a global event.
// Monitor touch operation
document.body.addEventListener("touchstart",function(){
// todo
})
or
// Monitor click operation
document.body.addEventListener("mousedown",function(){
// todo
})
Obtain the remote stream through the startPlayingStream interface, assign the media stream to the "srcObject" property of the media tag, and set the "muted" property of the corresponding media tag to true.
const remoteStream = await zg.startPlayingStream(streamID);
// video is a local <video> or <audio> object
video.srcObject = remoteStream;
video.muted = true;
In the event callback registered in the first step, set the "muted" property of the media tag to false, and the page will automatically switch to audio media for playback when it detects interactive operations.
video.muted = false;
Make sure that the user interacts with the page before setting the media stream to the media label to bypass Autoplay restrictions.
For desktop browsers, this solution can bypass most Autoplay restrictions, but on Safari/Webview, the autoplay policy will be more strict. If you need to be compatible with Safari/WebView, the following special treatment is recommended.
Safari only allows user interaction to trigger the playback of audio media, instead of turning on Autoplay restriction after user interaction.
When the media stream is playing automatically, set "muted" to true.
<video id="video" autoplay muted playsinline></video>
When each media stream is playing, specify the corresponding HTMLElement container to bind the interaction event (click or touch).
const play = document.getElementById('play');
play.addEventListener('click', () => {
});
On the playback interface, the icon shows that the current media stream is muted and guides the user to click.
<div id="play">Play</div>
When the user clicks on the playback interface of a media stream (the container just bound), play the stream again in the event callback, set "muted" to false, and change the mute icon.
video.muted = false;