Playing sound/audio on Javascript

Posted under » JavaScript on 15 June 2024

Simply this

var audio = new Audio('kkjlky.mp3');
audio.play();

Let's add a button and pause to it.

<audio id="myAudio">
   <source src="kkjlky.mp3" type="audio/mpeg">
   Your browser does not support the audio element.
</audio>

<p>Click the buttons to play or pause the audio.

<button onclick="playAudio()" type="button">Play Audio
<button onclick="pauseAudio()" type="button">Pause Audio 
<script>
 var x = document.getElementById("myAudio"); 
function playAudio() { 
	  x.play(); 
} 

function pauseAudio() { 
	  x.pause(); 
} 
</script>

web security linux ubuntu python django git Raspberry apache mysql php drupal cake javascript css AWS data