How to make audio autoplay on chrome

后端 未结 17 1553
南旧
南旧 2020-11-22 08:42

audio autoplay working in Mozilla, Microsoft edge and old google chrome as well but not in new google chrome. they have blocked the autoplay. is there any way to make it au

相关标签:
17条回答
  • 2020-11-22 09:04

    You could use <iframe src="link/to/file.mp3" allow="autoplay">, if the origin has an autoplay permission. More info here.

    0 讨论(0)
  • 2020-11-22 09:07

    As of April 2018, Chrome's autoplay policies changed:

    "Chrome's autoplay policies are simple:

    • Muted autoplay is always allowed.

    Autoplay with sound is allowed if:

    • User has interacted with the domain (click, tap, etc.).
    • On desktop, the user's Media Engagement Index threshold has been crossed, meaning the user has previously play video with sound.
    • On mobile, the user has added the site to his or her home screen.

    Also

    • Top frames can delegate autoplay permission to their iframes to allow autoplay with sound. "

    Chrome's developer site has more information, including some programming examples, which can be found here: https://developers.google.com/web/updates/2017/09/autoplay-policy-changes

    0 讨论(0)
  • 2020-11-22 09:07

    You may simply use (.autoplay = true;) as following (tested on Chrome Desktop):

    <audio id="audioID" loop> <source src="path/audio.mp3"  type="audio/mp3"></audio>
    
    <script>
    var myaudio = document.getElementById("audioID").autoplay = true;
    </script>
    

    If you need to add stop/play buttons:

    <button onclick="play()" type="button">playbutton</button>
    <button onclick="stop()" type="button">stopbutton</button>
    
    <audio id="audioID" autoplay loop> <source src="path/audio.mp3"  type="audio/mp3"> 
    </audio>
    
    <script>
    var myaudio = document.getElementById("audioID");
    
    function play() { 
    return myaudio.play(); 
    };
    
    function stop() {
    return myaudio.pause(); 
    };
    </script>
    

    If you want stop/play to be one single button:

    <button onclick="PlayStop()" type="button">button</button>
    
    
    <audio id="audioID" autoplay loop> <source src="path/audio.mp3"  type="audio/mp3"> 
    </audio>
    
    <script>
    var myaudio = document.getElementById("audioID");
    
    function PlayStop() { 
    return myaudio.paused ? myaudio.play() : myaudio.pause();
    };
    </script>
    

    If you want to display stop/play on the same button:

    <button onclick="PlayStop()" type="button">Play</button>
    
    
    <audio id="audioID" autoplay loop> <source src="path/audio.mp3"  type="audio/mp3"> 
    </audio>
    
    <script>
    var myaudio = document.getElementById("audioID");
    
    function PlayStop() { 
    if (elem.innerText=="Play") {
        elem.innerText = "Stop";
    }
    else {
        elem.innerText = "Play";
    }
    return myaudio.paused ? myaudio.play() : myaudio.pause();
    };`
    </script>
    

    In some browsers audio may doesn't work correctly, so as a trick try adding iframe before your code:

    <iframe src="dummy.mp3" allow="autoplay" id="audio" style="display:none"></iframe>
    
    <button onclick="PlayStop()" type="button">Play</button>
    
    
    <audio id="audioID" autoplay loop> <source src="path/audio.mp3"  type="audio/mp3"> 
    </audio>
    
    <script>
    var myaudio = document.getElementById("audioID");
    
    function button() { 
    if (elem.innerText=="Play") {
        elem.innerText = "Stop";
    }
    else {
        elem.innerText = "Play";
    }
    return myaudio.paused ? myaudio.play() : myaudio.pause();
    };
    </script>
    
    0 讨论(0)
  • 2020-11-22 09:10

    Solution #1

    My solution here is to create an iframe

    <iframe src="audio/source.mp3" allow="autoplay" style="display:none" id="iframeAudio">
    </iframe> 
    

    and audio tag aswell for non-chrome browsers

    <audio autoplay loop  id="playAudio">
        <source src="audio/source.mp3">
    </audio>
    

    and in my script

      var isChrome = /Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor);
      if (!isChrome){
          $('#iframeAudio').remove()
      }
      else {
          $('#playAudio').remove() // just to make sure that it will not have 2x audio in the background 
      }
    

    Solution #2:

    There is also another workaround for this according to @Leonard

    Create an iframe that doesn't play anything just to trigger the autoplay in the first load.

    <iframe src="silence.mp3" allow="autoplay" id="audio" style="display: none"></iframe>
    

    good source for the mp3 file silence.mp3

    Then play your real audio file at ease.

    <audio id="player" autoplay loop>
        <source src="audio/source.mp3" type="audio/mp3">
    </audio>
    

    Personally I prefer solution #2 because it is cleaner approach for not relying so much in JavaScript.

    Update August 2019

    Solution #3

    As an alternative we can use <embed>

    For Firefox It seems that audio auto-play is working so we don't need the <embed> element because it will create double audio running.

    // index.js
    let audioPlaying = true,
        backgroundAudio, browser;
    browser = navigator.userAgent.toLowerCase();
    $('<audio class="audio1" src="audio.mp3" loop></audio>').prependTo('body');
    if (!browser.indexOf('firefox') > -1) {
        $('<embed id="background-audio" src="audio.mp3" autostart="1"></embed>').prependTo('body');
        backgroundAudio = setInterval(function() {
            $("#background-audio").remove();
            $('<embed id="background-audio" src="audio.mp3"></embed>').prependTo('body');
        }, 120000); // 120000 is the duration of your audio which in this case 2 mins.
    }
    

    Also if you have a toggle event for your audio make sure to remove the created <embed> element for audio.

    Note: After your toggle, it will restart from the beginning because the <embed> is already deleted and the <audio> element will play as normal now.

    $(".toggle-audio").on('click', function(event) {
        audioPlaying = !audioPlaying;
        $("#background-audio").remove();
    
        clearInterval(backgroundAudio);
        if (audioPlaying){
            $(".audio1").play();
            // play audio 
        }
        else {
            $(".audio1").pause();
        }
    

    And now make sure to hide these <audio> and <embed> elements

    audio, embed {
        position: absolute;
        z-index: -9999;
    }
    

    Note: diplay: none and visibility: hidden will make the <embed> element not work.

    0 讨论(0)
  • 2020-11-22 09:10

    The default HTML5 audio autoplay attribute is not working in chrome, but you can force audio autoplay using JavaScript. Try this:

    document.getElementById('myAudio').play();
    

    This works for me.

    0 讨论(0)
  • 2020-11-22 09:12

    At least you can use this:

    document.addEventListener('click', musicPlay);
    function musicPlay() {
        document.getElementById('ID').play();
        document.removeEventListener('click', musicPlay);
    }
    

    The music starts when the user clicks anywhere at the page.

    It removes also instantly the EventListener, so if you use the audio controls the user can mute or pause it and the music doesn't start again when he clicks somewhere else..

    0 讨论(0)
提交回复
热议问题