Home > Mobile >  Tried a way to make the webpage play the background music automatically but it doesn't seem to
Tried a way to make the webpage play the background music automatically but it doesn't seem to

Time:01-21

Currently in the project, I hope to be able to play background music as soon as you enter the webpage, but chrome seems to have banned this!

Find a way to crack on the Internet, the reference website is as follows: http://www.nooong.com/docs /chrome_video_autoplay.htm

But I tried to use the above method by myself, the effect is still useless. I would like to ask you to help me to see if the spelling is wrong, or is it useless at all?

Maybe you have had similar needs, I hope you can share with me, I will be very grateful for your warm help, thank you again Everyone watch my question.

 let trigger = document.querySelector('.trigger');
    let autoplay = document.querySelector('.autoplay');
    trigger.onload = function(){
        autoplay.src = 'demo.mp3';
        autoplay.oncanplay = function(){
        autoplay.play();
        };
    };
<!-- background music to play -->
    <audio autoplay="autoplay" controls="controls" loop ="loop" preload ="auto"  >
        <source src="demo.mp3" />
    </audio>

    <!-- First trigger the audio in the iframe, put an empty music file in it -->
    <iframe allow="autoplay" style="display:none"   src="demo.mp3"></iframe>

CodePudding user response:

Here's another thread that appears to have found a couple workarounds: How to make audio autoplay on chrome

CodePudding user response:

Currently in the project, I hope to be able to play background music as soon as you enter the webpage, but chrome seems to have banned this!

Chrome, and most other browsers.

Find a way to crack on the Internet, the reference website is as follows:

It isn't possible. The site you're looking at doesn't do it either. At best, you can play on some trusted user action, which is indeed the workaround proposed at the site you mention.

But I tried to use the above method by myself, the effect is still useless. I would like to ask you to help me to see if the spelling is wrong, or is it useless at all?

That's because there's nothing you can do about this problem. You can't play music as soon as a page is opened.

CodePudding user response:

maybe: <audio autoplay controls><source src="demo.mp3" type="audio/mpeg"></audio>

  •  Tags:  
  • Related