-2

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"  class="autoplay">
        <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"  class="trigger" src="demo.mp3"></iframe>
AWEI
  • 417
  • 3
  • 9
  • 2
    Note that, as as the page you linked says, there is a reason why it is restricted: "Chrome这样做的目的是为了防止开发者滥用自动播放功能而对用户产生骚扰。" (Chrome did this to prevent harassment of users). If you do this and I somehow accidentally stumble onto your page, I will never, ever come back to it. That said, see [this page](https://developer.chrome.com/blog/autoplay/) for the exact rules regarding iframes and autoplay in Chrome. – Amadan Jan 21 '22 at 01:50

1 Answers1

1

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.

Brad
  • 159,648
  • 54
  • 349
  • 530