Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How to use JavaScript and HTML5 to play Sounds / Audio files.

To play sounds in JavaScript, you can use the HTML5 Audio API. Here is a basic example of how to use it to play a sound:


  
    Play Sound in JavaScript
    

In this example, we have an HTML button with an onclick event that calls the playSound() function when clicked. The playSound() function creates a new Audio object with the path to the sound file, and then calls the play() method to play the sound.

You can replace 'sound.mp3' with the path to your own sound file, such as 'https://yourdomain.com/sound.mp3'.

Note that not all browsers support all audio formats. You may need to provide multiple audio formats (e.g. MP3, OGG, WAV) to ensure compatibility across different browsers.

The post How to use JavaScript and HTML5 to play Sounds / Audio files. appeared first on Tutorials Made.



This post first appeared on TutorialsMade - Ultimate Tutorial, please read the originial post: here

Share the post

How to use JavaScript and HTML5 to play Sounds / Audio files.

×

Subscribe to Tutorialsmade - Ultimate Tutorial

Get updates delivered right to your inbox!

Thank you for your subscription

×