HTML (HyperText Markup Language) is a core language used to structure content on the web. It uses tags and elements to organise text, images, links, and media, such as audio. To insert sound into an HTML document, you can use the
Characteristics
Values
Embed audio
Use the <audio> tag
Supported audio formats
MP3, WAV, OGG
Location of audio file
Use the src attribute
Multiple audio sources
Use the <source> tag
Autoplay
Use the autoplay attribute
Loop
Use the loop attribute
Muted
Use the muted attribute
Customise audio controls
Use JavaScript and the HTMLMediaElement API
Controls
Play, pause, volume, progress bar
Fallback content
Provide download link for browsers that don't support <audio>
The HTML tag is an essential tool for embedding sound content in documents and adding sound elements to your webpage. It is used to embed sound content in a document, such as music or other audio streams. The tag contains one or more tags with different audio sources. The browser will choose the first source it supports and the most suitable one. The text between the and tags will only be displayed in browsers that do not support the element.
There are three supported audio formats in HTML: MP3, WAV, and OGG. When using HTTP(S) URLs, be aware that the browser's caching behaviour will affect how often the file is requested from the server. Data URLs embed the audio data directly in the HTML, which can be useful for small audio files but is not recommended for larger files as it increases the HTML file size.
The tag also supports the Global Attributes and Event Attributes in HTML. You can use the HTML audio tag autoplay attribute to play background music or sound effects automatically. To display captions, you will have to find a library or framework that provides the capability, or write the code yourself. One option is to play your audio using a
In addition to spoken dialogue, subtitles and transcripts should also identify music and sound effects that communicate important information. Synchronized transcriptions of dialogue or descriptions of significant sounds can help people who cannot hear the audio understand what is going on.
To add audio to an HTML webpage, you need to use the tag. This tag is used to embed a media player that supports audio playback into the HTML page.
The tag is used in conjunction with the tag to specify the audio file source. The tag provides the URL of the audio file to be played. Here is an example:
Html
In the above example, the audio file "audio.mp3" is located in the same directory as the HTML file. The "controls" attribute adds playback controls to the audio player, allowing the user to play, pause, and adjust the volume.
You can also provide multiple audio sources with different formats inside the tag. This is useful because not all browsers support the same audio formats. Here is an example:
Html
In this example, the browser will first look for the OGG format and, if unsupported, will move to the next tag and play the MP3 file.
The tag also supports other attributes such as "autoplay", "loop", "muted", and "preload" to customize the audio player's behaviour. For example:
Html
This code will make the audio play automatically, loop indefinitely, and start with the volume muted.
To include controls in your HTML audio player, you need to use the `` tag along with the controls attribute. This will add default audio controls to the element, allowing the user to play, pause, adjust the volume, and seek through the audio file. Here's an example:
Html
In the above code, the controls attribute is added within the `` tag. The `` tag specifies the location of the audio file (src) and its type. You can also include multiple `` tags to provide alternative audio formats, and the browser will choose the first recognised format.
If you want more customisation over the controls, you can create your own using JavaScript and the HTMLMediaElement API. This allows you to build a custom audio player with your own design and functionality. Here's an example of how you can use JavaScript to add play and pause functionality:
In this code, we first select the audio element, play button, and pause button using `querySelector`. Then, we add click event listeners to the play and pause buttons. When the play button is clicked, we use `audio.play()` to start the audio playback and disable the play and pause buttons accordingly. Similarly, when the pause button is clicked, we use `audio.pause()` to pause the audio and update the button disabled states.
By creating custom controls, you can style them however you like and include additional features such as a progress bar, volume slider, or playback speed controls.
To autoplay audio on an HTML webpage, you can use the `` tag along with the `` tag. The `` tag embeds a media player that supports audio playback into the HTML page, while the `` tag provides the URL of the audio file to be played.
Html
In the above code, the `autoplay` attribute is added to the `` tag to enable autoplay. The `controls` attribute adds playback controls for the user, including volume, seeking, and pause/resume playback.
It is important to note that autoplaying audio can be an unwelcome surprise for users, and browsers often provide various forms of autoplay blocking. Therefore, it is recommended to use autoplay carefully and only when necessary. Additionally, you can add `muted` after `autoplay` to allow the audio to play automatically but without sound.
To improve compatibility across browsers, it is a good practice to provide multiple audio sources and specify the MIME type of each audio file using the `type` attribute in the `` tag. For example:
To loop audio in HTML, you can use the `` tag with the loop attribute. This will cause the audio file to start over from the beginning when it reaches the end, creating a seamless loop. Here's an example of the code:
Html
In the above code, we have two audio sources specified using the `` tag. The controls attribute adds playback controls, allowing the user to play, pause, and adjust the volume. The `loop` attribute is what enables the audio to restart automatically when it finishes playing. You can include multiple audio formats, as shown above, and the browser will choose the first supported format.
Here's another example with a specific audio file:
Html
In this case, the audio file is named "horse.ogg," and it will loop continuously due to the `loop` attribute.
It's important to note that the `loop` attribute is a boolean attribute, and its presence alone specifies that the audio will restart each time it finishes playing. This attribute was introduced in HTML 5, enhancing the capabilities of web developers to create engaging and dynamic audio experiences on their web pages.