HTML
HTML introduction
HTML basics
HTML text format 1
HTML text format 2
HTML text format 3
HTML links
HTML images
HTML image maps
HTML forms
Fieldset/legend tags
Email links
HTML labels
HTML tables
HTML frames
HTML backgrounds
HTML colors
HTML color shades
HTML color usage
HTML marquees
HTML fonts
HTML entities
HTML stylesheets
HTML layout
HTML div/span
HTML audio
HTML video
HTML objects
HTML d'load media
HTML meta tags
HTML scripts
HTML declarations
HTML head section
HTML document types
HTML tag rules
HTML things to avoid
URL formatting
Encoding and decoding
HTML use/access
HTML publish work
History of HTML
HTML summary

Programming

Programming intro
Java

Markup

First webpage guide
XHTML

Style & Layout

CSS

Browser scripting

Javascript
VBScript
AJAX

Server scripting

PHP
ASP

Making money online

Make money online

HTML audio

Text and graphics are good for presenting content on the web, but what if you can include audio on your webpages? Audio can make a webpage more interactive resulting in a more interesting and entertaining experience for the user.

NOTE: The audio files on this page may take a few minutes to load. Please be patient.

This lesson focuses on:

Audio formats

There are several audio formats you can use for audio on the web. Audio includes everything ranging from simple sounds to complex songs.

The WAVE Format

Originally developed by Microsoft and IBM, the WAVE format is supported by computers that are running Windows, and it is also supported by all the major browsers. To be able to play sounds in the WAVE format on non-Windows computers, a plug-in such as Adobe Flash may be needed. Audio files in the WAVE format will have a .wav file extension.

The MP3 Format

A very popular format that is used extensively for music. This format utilizes strong compression (resulting in small file size) together with high quality. The MP3 format is supported by all major browsers. Audio files in the MP3 format will have a .mp3 file extension.

The MIDI Format

MIDI stands for Musical Instrument Digital Interface. This is a special format that is used for electronic music devices (like synthesizes or electric keyboards) to be able to communicate with computers.

This format was originally developed by the music industry. It can be used for a wide range of music making ranging from the simplest (a few notes) to the most complex (full professional songs).

The MIDI format is supported by all major browsers. Audio files in the MIDI format have a .mid file extension.

Playing audio with the <embed> tag

The <embed> tag can be used to place various media like audio and video on webpages.

NOTE: The <embed> tag is not part of the HTML 4.01 specifications. Despite this, it is still widely used and supported by modern browsers.

NOTE: The <embed> tag has no end tag.

Playing audio with <embed> example:

<embed src="/sounds/chords.wav" />

Output:

Click on the play button to play the sound.

The above example is very basic and uses just one attribute of the <embed> tag -- the src attribute which is the only required attribute. Below you will find a list of attributes for the <embed> tag as well as some examples for each of the audio formats mentioned earlier in this lesson.

Attributes of the <embed> tag
  • src - Denotes the URL of an audio file. This is the only required attribute of the <embed> tag.

  • width - Specifies the width of the control panel for the file.

  • height - Specifies the height of the control panel for the file.

  • hidden - Takes the value of 'true' or 'false' to specify if the control panel should be displayed or not. The default value is false.

  • autoStart - Takes the value of 'true' or 'false' to specify if the file should start playing automatically or not. The default value is false.

  • loop - Takes the value of 'true' or 'false' to specify if the file should loop continuously or not. The default value is false.

  • volume - Takes a numeric value between 0 - 100 signifying the volume of the audio file playing.

Playing a file in WAVE format with <embed>

Example:

<embed src="/sounds/chords.wav" width="200" height="60" autoStart="false" />

Output:

Playing a file in MP3 format with <embed>

Example:

<embed src="/sounds/notes.mp3" width="220" height="60" volume="85" autoStart="false" />

Output:

Playing a file in MIDI format with <embed>

Example:

<embed src="/sounds/bach.mid" width="250" height="60" volume="55" playcount="3" autoStart="false" />

Output:

A word of caution when using the <embed> tag:

The <embed> tag is not part of the HTML 4.01 specification. Using it will cause a webpage to not validate with a page validator. Even if it is widely used, using it is still not semantically correct. As an alternative, please use the <object> tag which we will discuss right now....

Playing audio with the <object> tag

The alternative to the <embed> tag is the <object> tag. Unlike the <embed> tag, the <object> tag is part of the HTML 4.01 specification and you can create valid webpages with it.

Playing audio with the <object> tag is a little more complicated and actually requires use of another tag -- the <param> tag.

Playing audio with the <object> tag example:

<object type="audio/x-wav" data="/sounds/chords.wav" width="200" height="40"> <param name="src" value="data/test.wav" /> <param name="autoplay" value="false" /> <param name="autoStart" value="0" /> listen to the audio: <a href="/sounds/chords.wav">chords.wav</a> </object>

Output:

listen to the audio: chords.wav

You will have to use the <object> tag together with the <param> and understand how they work together to play audio files with the <object> tag.

The <object> tag is used to actually embed the audio file on the webpage.

Attributes of the <object> tag
  • type - Denotes the MIME type of the object that will be embedded (in this case an audio file). MIME stands for Multimedia Internet Mail Extensions. A MIME type is used to specify a special code for the type of information that a file will contain. For WAVE format, use "audio/x-wav", for MP3 format, use "audio/mpeg", and for MIDI format, use "audio/rtp-midi"

  • data - Denotes the location of the audio file.

  • name - Specifies a name for the audio file object.

  • width - Specifies the width of the control panel for the audio file.

  • height - Specifies the height of the control panel for the audio file.

The <param> tag is used to pass parameters to the audio file object such as if it should start playing automatically.

Attributes of the <param> tag
  • name - Some detail about the audio file (such as "autoStart") that will be playing.

  • value - The value for this detail. For example, if the name attribute has the value "autoStart", you can give the value attribute the value "false"

NOTE: The <param> tag does not have a closing tag.

The above example, but this time with comments to make it clearer:

<--Create an object of type audio/x-wav (WAVE format audio file) the name of the file is chords.wav and it is located in the /sounds directory --> <object type="audio/x-wav" data="/sounds/chords.wav" width="200" height="40"> <-- the parameters for the audio file are specified with the <param> tag it should be placed inside the <object> tag and you can use it as many times as you need we set three parameters the "src" parameter is used even though it was already specified with the "data" attribute in the <object> tag because some browsers don't read the "data" attribute The "autoplay" parameter is set to false so that it will not play automatically. This parameter is understood by QuickTime The "autoStart" parameter is set to 0 so that it will not play automatically. This parameter is understood by Windows media Player and Real Audio. This is why we needed both an "autoplay" and "autoStart" parameter. So that several media players would get the correct instruction --> <param name="src" value="/sounds/chords.wav"> <param name="autoplay" value="false"> <param name="autoStart" value="0"> <-- what is the purpose of this data below? it is alternative content to display in case the audio file doesn't load it should go right before </object> --> listen to the audio: <a href="/sounds/chords.wav">chords.wav</a> </object>

Output:

listen to the audio: chords.wav

Playing an audio file in WAVE format with <object>

Example:

<object type="audio/x-wav" data="/sounds/chords.wav" width="200" height="40"> <param name="src" value="data/test.wav"> <param name="autoplay" value="false"> <param name="autoStart" value="0"> listen to the audio: <a href="/sounds/chords.wav">chords.wav</a> </object>

Output:

listen to the audio: chords.wav

Playing an audio file in MP3 format with <object>

Example:

<object type="audio/mpeg" data="/sounds/notes.mp3" width="200" height="40"> <param name="src" value="/sounds/notes.mp3"> <param name="autoplay" value="false"> <param name="autoStart" value="0"> listen to the audio: <a href="/sounds/notes.mp3">notes.mp3</a> </object>

Output:

listen to the audio: notes.mp3

Playing an audio file in MIDI format with <object>

Example:

<object type="audio/audio/rtp-midi" data="/sounds/bach.mid" width="200" height="40"> <param name="src" value="/sounds/bach.mid"> <param name="autoplay" value="false"> <param name="autoStart" value="0"> listen to the audio: <a href="/sounds/bach.mid">bach.mid</a> </object>

Output:

listen to the audio: bach.mid

Loading audio without a webpage

Loading audio without a webpage is easy. To do so, just put the absolute location of the audio file into your browser. For example, the URL for one of our audio files is http://www.landofcode.com/sounds/notes.mp3. Put this URL into your browser and you will see just the control for the audio object.

To provide audio to the user without a webpage, you can have a link on a webpage with the same absolute URL

Example:

<a href="http://www.landofcode.com/sounds/notes.mp3"> Click here to hear some notes </a>

Output:

Click on the above link to see the audio object in a browser by itself without a webpage.

NOTE: Sometimes the audio object will not load in the browser by itself, instead a box will appear asking you if you want to download the audio.

Things to avoid

When it comes to playing audio on a webpage, there are certain things that should definitely be avoided.

  1. Don't let any audio start playing automatically when the page loads
    Besides being a bad usability practice and a habit of myspace.com users (someone might have their speakers on full volume and suddenly music starts blasting out of nowhere), it is very annoying. Because to shut it off, the user has to scour your webpage to find the control to do so. They can just go ahead and shut off their own volume, but what if they need it or were using it for something else at the moment? This is a bad usability practice and should definitely be avoided.

  2. Don't hide the control for the audio file object
    The volume that you set initially may not be to someones liking, someone might want to stop the audio or start over half way through the audio, there are many possibilities. Having that control for the user is an important thing. Do not hide it.

Practice

Online code editor
Practical examples
Practical exercises
Step-by-step tutorials

Reference

Terms glossary
Reference material

Rate this site

Rate this site
Visitor comments