Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

HTML Audio

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

HTML5

The

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

Example

Try it »

Problem:

  • You must convert the audio file to different formats.

The Best HTML Solution

The following example uses two different audio formats. The HTML5

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 (
New Specifies subtitle files or other text-containing files for the media element (