html audio play code example

Example 1: javascript play audio

//play audio with from html audio element: 
document.getElementById('myAudioTagID').play();

//play audio with out html audio tag
var myAudio = new Audio('my_great_song.mp3');
myAudio.play();

Example 2: how do you play audio files on html

<audio src="sound.mp3" autoplay></audio>

Example 3: how play audio js

let myAudioElement = new Audio('audio.mp3');
myAudioElement.addEventListener("canplaythrough", event => {
  /* the audio is now playable; play it if permissions allow */
  myAudioElement.play();
});

Example 4: play audio source on html

<audio controls>
  <source src="myAudio.mp3" type="audio/mpeg">
  <source src="myAudio.ogg" type="audio/ogg">
  <p>Your browser doesn't support HTML5 audio. Here is
     a <a href="myAudio.mp4">link to the audio</a> instead.</p>
</audio>