DEV Community

Cover image for Play audio with HTMLAudioElement API in JavaScript
Amitav Mishra
Amitav Mishra

Posted on • Originally published at jscurious.com

Play audio with HTMLAudioElement API in JavaScript

The HTMLAudioElement API has access to all the properties and methods of the HTML <audio> element. This API allows us to play audio without even creating the <audio> elements. The Audio() constructor creates and returns a new HTMLAudioElement object.

const audio = new Audio('nature.wav');
Enter fullscreen mode Exit fullscreen mode

This API inherits properties and methods from the HTMLMediaElement API that are needed to use the media capabilities like play, pause, mute, etc. Let’s discuss some of them.

Methods

The play() method

This method is used to play the audio. It returns a Promise which resolves when the audio successfully starts and rejects if any error occurs in starting the audio.

const audio = new Audio('nature.wav');
audio.play();
Enter fullscreen mode Exit fullscreen mode

The pause() method

This method pauses the audio.

audio.pause();
Enter fullscreen mode Exit fullscreen mode

The load() method

This method loads or resets the audio. If we call this method, the audio will start from the beginning when we play it next time.

audio.load();
Enter fullscreen mode Exit fullscreen mode

Use case 1 – Load:

When we create a new HTMLAudioElement object and call the play() method for the first time, it takes some time to load and play the audio. So, for a scenario where we instantly want to play the audio on a button click or something, we see a delay.

To fix this delay, we can use the load() method to load the audio beforehand and keep it ready, so the next time we call play(), it will start playing without any delay.

const audio = new Audio('nature.wav');
audio.load();

// now play the audio whenever you want
audio.play();
Enter fullscreen mode Exit fullscreen mode

Use case 2 – Reset:

If we want to stop the ongoing audio, and we want it to play from the beginning the next time we play it, then we can use the load() method to reset the audio after we pause it.

audio.pause();
audio.load();
Enter fullscreen mode Exit fullscreen mode

Properties

duration

It returns the total duration of the audio in seconds.

console.log(audio.duration);
Enter fullscreen mode Exit fullscreen mode

loop

It is used to set or get the loop value that indicates whether the audio should play repeatedly.

const audio = new Audio('nature.wav');
console.log(audio.loop); // false
audio.loop = true;
console.log(audio.loop); // true
Enter fullscreen mode Exit fullscreen mode

muted

It is used to set or get the mute value of the audio.

const audio = new Audio('nature.wav');
console.log(audio.muted); // false
audio.muted = true;
console.log(audio.muted); // true
Enter fullscreen mode Exit fullscreen mode

volume

It is used to set or get the volume of the audio. Its value ranges from 0.0 to 1.0. If we set any value higher than 1, it throws an error.

const audio = new Audio('nature.wav');
console.log(audio.volume); // 1
audio.volume = 0.1;
Enter fullscreen mode Exit fullscreen mode

You may also like


Thanks for your time ❤️
Find more of my writings on web development at jscurious.com

Top comments (0)