HTML Audio
Sound can be played in different ways in HTML.
Problems and Solutions
Playing audio in HTML is not easy!
You need to master a number of techniques to ensure that your audio files will play in all browsers (Internet Explorer, Chrome, Firefox, Safari, Opera) and on all hardware (PC, Mac, iPad, iPhone).
In this chapter, Runoob Tutorial summarizes the problems and solutions for you.
Use plugins
A browser plug-in is a small computer program that extends the standard functionality of a browser.
Plug-ins can be added to a page using tags or tags.
These tags define containers for resources (usually non-HTML resources). Depending on the type, they will be displayed either by the browser or by an external plug-in.
Use Element
The tag defines a container for external (non-HTML) content. (This is an HTML5 tag; it is invalid in HTML4, but works in all browsers).
The following code snippet can display an MP3 file embedded in a web page:
Example
Try it »
Problem:
The tag is invalid in HTML 4. The page cannot be validated as HTML 4.
Different browsers have different support for audio formats.
If the browser does not support the file format, the audio cannot be played without a plug-in.
If the user's computer does not have a plug-in installed, the audio cannot be played.
Even if the file is converted to other formats, it still cannot be played in all browsers.
Use Element
The tag can also define a container for external (non-HTML) content.
The following code snippet can display an MP3 file embedded in a web page:
Example
Try it »
Problem:
Different browsers have different support for audio formats.
If the browser does not support the file format, the audio cannot be played without a plug-in.
If the user's computer does not have a plug-in installed, the audio cannot be played.
Even if the file is converted to other formats, it still cannot be played in all browsers.
Using HTML5 Element
HTML5 The element is an HTML5 element; it is invalid in HTML 4, but works in all browsers.
The element works in all modern browsers.
Browser Compatibility
The numbers in the cells indicate the first browser version that supports the attribute.
Element
4.0
9.0
3.5
4.0
10.5
Below we will use The tag to describe the MP3 file (valid in Internet Explorer, Chrome, and Safari), and an OGG type file is also added (valid in Firefox and Opera). If this fails, it displays an error text message:
Example
Your browser does not support this audio format.
Try it »
Problem:
The tag is invalid in HTML 4. Your page cannot be validated as HTML 4.
You must convert the audio file to different formats.
The element does not work in older browsers.
The Best HTML Solution
The following example uses two different audio formats. The HTML5 element will try to play the audio in mp3 or ogg. If that fails, the code falls back to trying .
Example
Try it »
Problem:
You must convert the audio to different formats.
The element cannot fall back to display an error message.
Using Hyperlinks
If a web page contains a hyperlink to a media file, most browsers will use a "helper application" to play the file.
The following code snippet shows a link to an mp3 file. If the user clicks the link, the browser will launch a "helper application" to play the file:
Example
Play the sound
Try it »
Inline Sound Description
When you include sound in a web page, or as part of a web page, it is called inline sound.
If you plan to use inline sound in a web application, you need to be aware that many people find inline sound annoying. Also note that users may have turned off the inline sound option in their browser.
Our best advice is to include inline sound only where the user wants to hear it. A positive example is when a user needs to hear a recording and clicks a link, which opens a page and then plays the recording.
HTML Multimedia Tags
New : HTML5 New Tags
tag
Description
Defines an embedded object. Deprecated in HTML4, allowed in HTML5.
Defines an embedded object.
Defines a parameter for an object.
New
Defines sound content
New
Defines a video or movie
New
Defines multimedia resources for the media element ( and )
New
Specifies subtitle files or other text-containing files for the media element ( and)