Auto Play HTML5 Audio Element & Animation

- 1 answer

Ad

I am working with a code that controls a very basic html5 player with animated elements. As is, the code starts the audio and the animation when clicked. I would like for the audio and animation to begin automatically as soon as the page loads. Any help would be greatly appreciated. Thanks!

    $( document ).ready(function() {
      $('.spinner-wrap').click(function() {

var $this = $(this),
    audio = $this.siblings('audio')[0],
    bpm = Number($this.siblings('audio').data('bpm'))
    pulse = (60/bpm)*1000;


if (audio.paused === false) {
  audio.pause();
  audio.currentTime = 0;
  $this.removeClass('playing');
  clearInterval(intervals);
}

else {
  audio.play();
  $this.addClass('playing');

}


function pulsing() {

  $this.addClass('pulse');

  setTimeout(function() {
    $this.removeClass('pulse');  
  }, pulse-100);

}


      });

    });
Ad

Answer

Ad

I suggest you use the autoplay attribute of the audio tag, like so:

<audio autoplay>
   <source src="ogg.ogg" type="audio/ogg">
   <source src="mp3.mp3" type="audio/mpeg">
   Sorry, no audio
</audio>
Ad
source: stackoverflow.com
Ad