JavaScript automatically play background music

Problem Description

Js error when the automatic control audio playback music:

Uncaught (in promise) DOMException

Play music error -201 992 704 146

My previous error codes:

<audio id="myaudio" src="assets/audio/芒种.mp3" controls="controls"  loop="true" hidden="true">
</audio>
$(function () {
    var myAuto = document.getElementById('myaudio');
    myAuto.play();
})

The reason is search the Internet, said:

This is because, Chrome only allows users to web pages can be automatically play audio and video post-active trigger. In fact, strictly speaking, is not allowed to play audio Chrome web pages before the user trigger, and the video is actually unlimited. But because the video file also contains audio, so it was banned together. Chrome purpose of this is to prevent abuse of the autoplay feature developers to produce the harassment of users.

Content from: http://www.nooong.com/docs/chrome_video_autoplay.htm

Solution:

Was added to the above html code autoplay, as follows:

<audio id="myaudio" src="assets/audio/芒种.mp3" controls="controls" autoplay loop="true" hidden="true">
</audio>

Then you can play properly, my version of Chrome:

Browser version -201,992,705,022

carry out

If you like it, please pay attention weyoung public number ...

 


wechat_qrcode-2019115195545

Guess you like

Origin www.cnblogs.com/nelucifer/p/11595429.html