• How to make audio autoplay on chrome


    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.
  • 0 comments:

    Post a Comment

    FAVOURITE LINE

    To steal ideas from one is plagiarism. To steal from many is Research.

    ADDRESS

    Mumbai , Maharashtra

    EMAIL

    shikha.pathak6@gmail.com
    shikha.the.swt.pari@gmail.com

    Skype

    shikha_pari