How to make audio autoplay on chrome

后端 未结 17 1595
南旧
南旧 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:15

    temp fix

    $(document).on('click', "#buttonStarter", function(evt)
    {
    var context = new AudioContext();
    document.getElementById('audioPlayer').play();
    $("#buttonStarter").hide()
    $("#Game").show()
    });
    

    Or use a custom player to trigger play http://zohararad.github.io/audio5js/

    Note : Autoplay will be renabled in 31 December

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

    The video tag can play audio as well. Given the audio tag doesn't seem to be behaving as it should be, you can just use the video tag for audio:

    <video autoplay muted id="audio1" src="your.mp3" type="audio/mp3">
        Your browser does not support the <code>video</code> element.
    </video>
    <script>
        unmuteButton.addEventListener('click', function() 
            { 
                if ( unmuteButton.innerHTML == "unmute" )
                {
                    unmuteButton.innerHTML = "mute";
                    audio1.muted = false; 
                } else {
                    unmuteButton.innerHTML = "unmute";
                    audio1.muted = true; 
                }
            });
    </script>
    
    0 讨论(0)
  • 2020-11-22 09:18

    Google changed their policies last month regarding auto-play inside Chrome. Please see this announcement.

    They do, however, allow auto-play if you are embedding a video and it is muted. You can add the muted property and it should allow the video to start playing.

    <video autoplay controls muted>
      <source src="movie.mp4" type="video/mp4">
      <source src="movie.ogg" type="video/ogg">
      Your browser does not support the video tag.
    </video>
    
    0 讨论(0)
  • 2020-11-22 09:20

    Just add this small script as depicted in https://developers.google.com/web/updates/2017/09/autoplay-policy-changes#webaudio

    <head>
    <script>
    window.onload = function() {
      var context = new AudioContext();
    }
    </script>
    </head>
    

    Than this will work as you want:

    <audio autoplay>
          <source src="hal_9000_sorry_dave.mp3">
    </audio>
    
    0 讨论(0)
  • 2020-11-22 09:21

    Solution without using iframe, or javascript:

    <embed src="silence.mp3" type="audio/mp3" autostart="true" hidden="true">
            <audio id="player" autoplay controls><source src="source/audio.mp3" type="audio/mp3"></audio>
    

    With this solution, the open/save dialog of Internet Explorer is also avoided.

       <embed src="http://deinesv.cf/silence.mp3" type="audio/mp3" autostart="true" hidden="true">
            <audio id="player" autoplay controls><source src="https://freemusicarchive.org/file/music/ccCommunity/Mild_Wild/a_Alright_Okay_b_See_Through/Mild_Wild_-_Alright_Okay.mp3" type="audio/mp3"></audio>

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

    The browsers have changed their privacy to autoplay video or audio due to Ads which is annoying. So you can just trick with below code.

    You can put any silent audio in the iframe.

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

    Just add an invisible iframe with an .mp3 as its source and allow="autoplay" before the audio element. As a result, the browser is tricked into starting any subsequent audio file. Or autoplay a video that isn’t muted.

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