autoplay test

by juberti

HTML

<button id="btn">Play</button>
<p>
Click button or press the "A" key(You need to press the result-tab before you press A)
</p>

<!-- Create an audio element and hide it with css: display:none -->
<audio id="audio" controls style="display:none">
  <source src="http://butlerccwebdev.net/support/html5-video/media/soundfile.mp3" type="audio/mpeg">
Your browser does not support the audio element.
</audio>

JavaScript

document.getElementById('btn').onclick = function(){
//Getting button and listning for click
document.getElementById('audio').play();
//Utilizing the HTML Audio/Video DOM Reference to play audio/video
}

//to play on the key A do this(using Jquery):
document.addEventListener('keydown', function(e) {
  if(e.keyCode == 65){
  document.getElementById('audio').play();
  }
});