Sounds can be played in HTML by many different methods.
Playing audio in HTML is not easy!
You must know a lot of tricks to make sure 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 W3Schools summarizes the problems and the solutions.
A plug-in is a small computer program that extends the standard functionality of the browser.
Plug-ins can be added to HTML pages using the <object> tag or the <embed> tag.
These tags define containers for resources (normally non-HTML resources), which, depending on the type, will either be displayed by the browsers, or by an external plug-in.
The <embed> tag defines a container for external (non-HTML) content.
The following code fragment should play an MP3 file embedded in a web page:
The <object tag> tag can also define a container for external (non-HTML) content.
The following code fragment should play an MP3 file embedded in a web page:
The HTML5 <audio> tag defines sound, such as music or other audio streams.
The <audio> element works in all modern browsers.
The following example uses the <audio> tag, and specifies one MP3 file (for Internet Explorer, Chrome, and Safari), and one OGG file (for Firefox and Opera). If anything fails it displays a text:
The example below uses the HTML5 <audio> element and tries to play the audio either as MP3 or OGG. If it fails, the code "falls back" to try the <embed> element:
The FREE Yahoo Media Player is definitely a favorite: You simply let Yahoo do the job of playing your songs.
It plays MP3 and a lot of other formats. You can add it to your page (or blog) with a single line of code, and easily turn your HTML page into a professional playlist:
To use it, insert the following JavaScript at the bottom of your web page:
Then, simply link to your audio files in your HTML, and the JavaScript code
automatically creates a play button for each song:
<a href="song1.mp3">Play Song 1</a>
<a href="song2.wav">Play Song 2</a>
...
...
The Yahoo Media Player presents your readers with a small play button instead of a full player. However, when you click the button, a full player pops up. Note that the player is always docked and ready at the bottom of the window. Just click on it to slide it out.
If a web page includes a hyperlink to a media file, most browsers will use a "helper application" to play the file.
The following code fragment displays a link to an MP3 file. If a user clicks on the link, the browser will launch a helper application to play the file:
When sound is included in a web page, or as part of a web page, it is called inline sound.
If you plan to use inline sounds, be aware that many people will find it annoying. Also note that some users might have turned off the inline sound option in their browser.
Our best advice is to include inline sounds only in pages where the user expects to hear sounds. An example of this is a page which opens after the user has clicked on a link to hear a recording.
New : New tags in HTML5.
| Tag | Description |
|---|---|
| <embed> | Defines an embedded object |
| <object> | Defines an embedded object |
| <param> | Defines a parameter for an object |
| <audio>New | Defines sound content |
| <video>New | Defines a video or movie |
| <source>New | Defines multiple media resources for media elements (<video> and <audio>) |
| <track>New | Defines text tracks for media elements (<video> and <audio>) |
Your message has been sent to W3Schools.