Solve the problem of google browser autoplay

After setting autoplay="true" in the video attribute of the page, when browsing with chrome, it is found that it will not play automatically. The reason is that chrome 66 released in April 2018 also officially announced to turn off automatic audio playback.

<audio id="demo" src="https://598.mp3" autoplay="true" controls loop style="display: none"></audio>

<script>
/*
* muted 静音
* autoplay 自动播放
* loop 循环播放
*/
var audio = document.getElementById('demo');
var t1 = 3e3;//如果是轮询,这个时间必须大于音频的长度。如果是webscoket,应该设置一个状态play,避免重复播放,如下:
var t2 = 2500;//音频的长度,确保能够完整的播放给用户
var play = false;
function run(){
    if(play){
        return false;
    }
    audio.currentTime = 0;//播放音频的起始时间
    audio.volume = 0.5;//音频声音大小
    audio.muted = false;//关闭静音状态
    play = true;
    setTimeout(function(){
        play = false;
        audio.muted = true;//播放完毕,开启静音状态
    },t2);
}
setInterval(function(){
    run();
},t1);
</script>

if it still doesn't work

image.png

 

image.png

 

image.png

 

Guess you like

Origin blog.csdn.net/m0_53574149/article/details/131807923