Categories
JavaScript Answers

How to play audio with JavaScript?

Spread the love

Sometimes, we want to play audio with JavaScript.

In this article, we’ll look at how to play audio with JavaScript.

How to play audio with JavaScript?

To play audio with JavaScript, we call the play method.

For instance, we write

const audio = new Audio("audio_file.mp3");
audio.play();

to create an audio element with the Audio constructor called with the path to the audio file.

Then we call play to play the audio file.

Conclusion

To play audio with JavaScript, we call the play method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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