Full width home advertisement

How To

Tech

JavaScript

Post Page Advertisement [Top]

HTML Sounds / Audio


HTML Sounds / Audio


Sounds can be embedded in HTML pages with several methods.

Problems, Problems, and Solutions

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.

Using Plug-ins

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.

HTML Audio - Using <embed>

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:

Example

<embed height="50" width="100" src="horse.mp3">

Problems:

  • Different browsers support different audio formats
  • If a browser does not support the file format, the audio will not play without a plug-in
  • If the plug-in is not installed on the users' computer, the audio will not play

HTML Audio -  Using <object>

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:

Example

<object height="50" width="100" data="horse.mp3"></object>

Problems:

  • Different browsers support different audio formats
  • If a browser does not support the file format, the audio will not play without a plug-in
  • If the plug-in is not installed on the users' computer, the audio will not play

The HTML5 <audio> Element

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 HTML5 <audio> tag, which specifies one MP3 file (for Internet Explorer, Chrome, Firefox 21+, and Safari), and one OGG file (for older Firefox and Opera). If something fails, it will display a text:

Example

<audio controls>
  <source src="horse.mp3" type="audio/mpeg">
  <source src="horse.ogg" type="audio/ogg">
  Your browser does not support this audio format.
</audio>

Problems:

  • You must convert the audio files into different formats
  • The <audio> element does not work in older browsers

HTML Audio - The Best Solution

The best solution is to use the HTML5 <audio> element + the <embed> element.
The example below uses the <audio> element and tries to play the audio either as MP3 or OGG. If that fails, the code "falls back" to try the <embed> element:

Example

<audio controls>
  <source src="horse.mp3" type="audio/mpeg">
  <source src="horse.ogg" type="audio/ogg">
  <embed height="50" width="100" src="horse.mp3">
</audio>

Problems:

  • You must convert the audio files into different formats
  • The <embed> element cannot "fall-back" to display an error message

HTML Audio - Using A Hyperlink

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:

Example

<a href="horse.mp3">Play the sound</a>


Tips About Inline Sounds

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.

HTML Multimedia Tags

New : New tags in HTML5.
TagDescription
<embed>Defines an embedded object
<object>Defines an embedded object
<param>Defines a parameter for an object
<audio>NewDefines sound content
<video>NewDefines a video or movie
<source>NewDefines multiple media resources for media elements (<video> and <audio>)
<track>NewDefines text tracks for media elements (<video> and <audio>)

Bottom Ad [Post Page]