Adding Audio to a Webpage
To add audio to a webpage, use the HTML audio element. This element allows you to embed sound files like music, podcasts, or sound effects directly into your document.
The audio element is the modern way to handle sound on the web, replacing older methods (like using the embed tag) that required browser plugins.
The Basic Syntax
In its simplest form, you can add an audio player like this:
Key Attributes
src: This specifies the URL of the audio file.controls: This boolean attribute tells the browser to display the standard audio controls (play/pause, volume, etc). Without it, the player will be invisible unless you use something like JavaScript to control it.autoplay: If present, the audio will start playing as soon as it can. That said, many browsers block autoplaying audio to avoid annoying users.loop: If present, the audio will start over again every time it finishes.
Interactive Example
Below is a working example of an audio player on a webpage:
Providing Multiple Formats
Not all browsers support every audio format. To ensure your audio plays for as many visitors as possible, you can provide multiple versions of your audio file using the source element:
In this case, the browser will choose the first format that it supports.