JavaScript

Audio play() Method in javascript

Definition and Usage

The play() method starts playing the current audio.

Tip: The audio() method is often used together with the pause() method.

Tip: Use the controls property to display audio controls (like play, pause, seeking, volume, etc, attached on the audio).

Syntax

audioObject.play()

Example

An audio player with play and pause buttons:

<!DOCTYPE html>
<html>
<body>

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

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

<button onclick="playAudio()" type="button">Play Audio</button>
<button onclick="pauseAudio()" type="button">Pause Audio</button> 

<script>
var x = document.getElementById("myAudio"); 

function playAudio() { 
  x.play(); 
} 

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

</body>
</html>

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button