Embed Sound in Your Page
Have you ever visited a Web site and found yourself listening to background music that started playing automatically? Or perhaps you heard a recorded greeting, welcoming you to the site? This is an easy way to begin using audio. As long as you keep the clips short, the resulting file size will be small and manageable. You won’t need to worry about long download times delaying your sounds.
Embed Files with <object>
As you’ll see later in this chapter, you can embed sound and video files with both the <bgsound> and <embed> elements. However, if you want to write “standards-compliant” XHTML, your method of choice should be the <object> element. The <object> element is in a class by itself. If you’ve worked through Chapter 10 on CSS, you learned about the <div> and <span> elements. They are generic elements that enable Web authors to apply styles apart from the limitations of more specific elements. <object> is also a generic element, but you use it for inserting objects in Web pages. You can define an “object” as being just about anything you want to put in a Web page. Its use is not restricted to audio or video files. You can use it instead of <img /> to insert pictures. The <object> element is also useful for including Active-X controls, Java applets, text, and even other Web pages in your page. This is truly an “all-purpose” element. Eventually, the <object> element will be the only way of embedding anything in a Web page.
Because <object> can embed many different types of objects, you must inform the browser just what kind of object you are putting in. To embed a sound file in a Web page with the object element, follow these steps:
Open template and save it as object.
Add the opening object tag:
<object>
Use the data=" " attribute to tell the browser where to find the file you want to embed. The value for this will be the URL of the file you want to embed:
<object data="greeting.wav">
Now that you’ve told the browser where to find the file, you must tell it what kind of file it will be embedding. For this, you use the type=" " attribute with the MIME type of the file. In this case, it will be audio/wav:
<object data="greeting.wav" type="audio/wav">
You should add the height and width attributes to specify the amount of space allotted to the plug-in:
<object data="greeting.wav" type="audio/wav"
height="125" width="125">
Add the autostart="true" attribute to allow the sound to play as soon as it loads:
<object data="greeting.wav" type="audio/wav"
height="125" width="125" autostart="true">
Add the closing tag. Even though the <object> element functions as an empty element, it still requires a closing tag:
<object data="greeting.wav" type="audio/wav"
height="125" width="125" autostart="true">
</object>
Just to give the page some substance, try adding a line that reads <h1>This page shows the <object> element in action</h1>. Your finished code should resemble the illustration that follows the XHTML:
<object data="greeting.wav" type="audio/wav"
height="125" width="150" autostart="true">
</object>
<h1>This page shows the <object><br />
element in action.</h1>

Tip | Any time you want to display an HTML tag as part of a Web page, you must enclose the text with the character entities for the “less than,” < , and “greater than,” > , signs. If you typed in only the tag, the browser would interpret it as an element and not as part of the text of your document. |
Understand MIME Types
In the preceding example, you had to identify what kind of file the browser should expect by using the type attribute. The value you supplied was called the MIME type. MIME stands for Multipurpose Internet Mail Extensions. It was developed as a means for enabling non-text files to be transferred by e-mail. When you use the <object> element to embed a sound or video file, you must identify the file by its MIME type. This includes a basic file type such as audio, image, video, and so on, followed by a slash and a more specific type (often the file’s normal extension). For example, the preceding entry was for a .wav file. In that case you entered type="audio/wav".
Tip | You can find a comprehensive list of MIME types at Table 11-2.
|
Tip | You can substitute any sound file you choose in place of . |
Add the autostart="false" attribute to tell the browser not to start the audio clip automatically:
<embed src=" autostart="false">
Specify the size of the “player” the browser will display by using the height=" " and width=" " attributes:
<embed src=" autostart="false"
height="20" width="125">
Add a closing tag:
<embed src=" autostart="false"
height="20" width="125"> </embed>
Caution | Even though embed works like an empty element (nothing in between the two tags), a closing tag is required. |
Your HTML for a page with an embedded sound file should resemble this code:
<html>
<head>
<title>Using the Embed Element</title>
</head>
<body>
<embed src=" autostart="false"
height="20" width="125"> </embed>
<h1>This is a sample of a page<br />
with an embedded sound</h1>
</body>
</html>
When you save your page and display it in a browser, you should see a set of controls in the upper-left corner of the browser window. These controls function like the controls on a tape recorder and allow the visitor to play and replay a sound.

Use <embed> for Background Sound
What if you want to create a background sound using the <embed> element? You merely have to add one attribute and modify another. You must add the loop attribute as you did with <bgsound/>. With <embed>, however, loop uses a different set of values. Instead of numerical values or infinite, here loops can be given a value of only true or false. False turns the loop off; true makes the sound loop endlessly. In addition to adding the loop attribute, you must change the value in the autostart attribute to true. This will cause the sound to begin automatically. You can also delete the height and width attributes, as they are not needed for a background sound. Your modified code would look like this:
<embed src=" autostart="true
loop="true"> </embed>.
Did You Know?—Background Sounds Can Be Quite Annoying
As a rule, background sounds tend to be very irritating to your visitors. More often than not, an endlessly looping background sound will drive people away from your site rather than draw them to it. Unless you have a good reason to include background sound, you're probably better off avoiding it. Remember, you don't want to include effects on your Web page just because they're “cool.” The whole idea is to get people to visit, hopefully more than once. If your sounds are an irritant, get rid of them.
Use <embed> and <bgsound /> Together for Maximum Compatibility
Because both <embed> and <bgsound /> are extensions that are not part of the official HTML specification, you might encounter compatibility problems with some browsers if you use only one of them. If you plan to use these elements to add background sound to your pages, it would be wise to use both of them. For example, if you were to modify the original page created in this chapter, bgsound, to include both <bgsound /> and <embed>, your code might look like the following:
<html>
<head>
<title>Bgsound Sample</title>
</head>
<body>
<h1>This is a sample of a page
with a background sound</h1>
<bgsound src=" loop="3" />
<embed src=" autostart="true"
loop="true"> </embed>.
</body>
</html>
Use <noembed> </noembed> for Non-Compatible Browsers
If you’re concerned about visitors to your site who might be using browsers that do not support the <embed> element, you can provide substitute content for them with the <noembed> element. Similar to the <noframes> element, <noembed> displays alternative content in browsers that do not recognize the <embed> element. You could include a textual description or a title for your video, thus providing your visitors with some idea of what they are missing.
Project 18: Embed Video in Web Pages
After you have embedded some audio files in an HTML document, adding video is easy. For this project, you will create three basic Web pages and insert video in each one by using a different means. Your primary tools will be the <object> and <embed> elements. However, an easier option to start off with is one that, unfortunately, works only with Internet Explorer.
Tip | To complete this project, you will need a video file. The easiest source for video files is your own computer. If you’re working with Windows, simply click Start | Find or Search. Choose Files or Folders in the pop-up menu. In the Named window, type *.avi or *.mov. Make sure that the Look In option is set to C:\ (or whatever drive letter you wish to search) and that the Include Sub-folders option is checked. Then click Find Now and you should quickly have a reasonable number of video files to choose from. If you don’t find any .avi files on your C:\ drive, be sure and perform the same type of search for any CD-ROMs (particularly games) that you may have. |
Add Video with the dynsrc Attribute (Internet Exporer Only)
Internet Explorer has provided an uncomplicated way to include video on a page without using either <object> or <embed>. You can add video files using the <img /> element with the dynamic source, dynsrc, attribute. Unfortunately, dynsrc is not supported beyond Internet Explorer, so you might not want to use it extensively. However, it’s an easy way to begin experimenting with video files. Besides, if you happen to be designing pages for a corporate intranet and you know everyone will be viewing your pages with IE, this is an easy way to add video to your pages.
Using the dynsrc=" " attribute to add video to a page is as easy as adding an image. If you have read Chapter 6 on adding graphics, you will remember that a graphical image can be embedded in a page with the <img /> (image) element. For a JPEG or GIF image, the element is written <img src="/image/library/english/10231_filename.jpg" />. To add a video instead of a still picture, use the dynamic source attribute instead of the source attribute. For example, to use dynsrc to add a video to a page, you would write the image element like this: <img dynsrc=">. The following HTML code creates a page with the file displaying as soon as the page is loaded:
<html>
<head>
<title>Use <em>dynsrc</em> for Video</title>
</head>
<body>
<img dynsrc=" />
<h1>This video is added with the<br />
dynsrc attribute.</h1>
</body>
</html>
When you display this page, you should see something like this:

Some other attributes you can add to work along with dynsrc=" " are these:
loop=" " As with <bgsound />, you can specify infinite to create an endless loop or a numerical value for the number of times you want the video to play.
start=" " This allows you to specify when the video should start playing. Your choices are fileopen or mouseover. With fileopen (the default), the video begins playing as soon as the file loads. With mouseover, the image will not play until the mouse moves over it. Try modifying the preceding file to read <img dynsrc=" start="mouseover" />. Save the file and load it. When the page has loaded, move your cursor over the image and see what happens.
Add Video with <object>
To add video with the <object> element, you proceed in much the same way as you would if you were embedding an audio file. The primary difference is that you use the data attribute to identify the file, you specify in the type attribute that you are using a video file, and you include the proper MIME type. For example, to insert the video from the preceding illustration with <object>, you would write the markup this way:
<html>
<head>
<title>Use <object> for Video</title>
</head>
<body>
<object data=" type="video/avi"
height="200" width="200" >
</object>
<h1>This video was added with the<br />
<object> element.</h1>
</body>
</html>
How to: Avoid Problems with Browsers that Don’t support <object>
The <object>element is a versatile tool that will eventually become a key player in XHTML. Because <object> can be used to embed any object in a page, it will eventually replace elements such as the image <img /> element. Unfortunately, many older browsers do not support <object>, thus limiting its usefulness. If you want to use <object> but also want to accommodate older browsers, simply nest the <imbed> element (along with the appropriate attributes) in between the <object> tags. Browsers that don’t support <object> will ignore the tags altogether and display the contents of the <embed> element. The nested elements would look like this:
<object data="
height="##" width ="##"
<embed src="
height="##" width ="##"
autostart="true" loop="false" >
</embed>
</object>
As in the preceding code, you can use the height and width attributes to specify the display size for your video. Keep in mind that for your average visitor, the larger the display, the poorer the quality. Thus, you need to keep your height and width attributes to a reasonably small size. In the preceding illustration, the values for those attributes were set to 200200 pixels.
Add Video with <embed>
The third alternative for embedding video on a page is Netscape’s <embed> element. This element is similar to <object>, but you need to use some different attributes. Instead of the data attribute, you use the src attribute to point to the video file. Also, with <embed>, you don’t need to use the type attribute. If you want the video to start automatically, use autostart="true". If not, make the value false. Also, a value of loop="true" causes the video to loop endlessly. False permits the video to loop only a single time. Use the following code to insert a video clip on a page with the <embed> element:
<html>
<head>
<title>Use <embed> for Video</title>
</head>
<body>
<embed src="
height="200" width="200"
autostart="true" loop="false" >
</embed>
<h1>This video was added with the<br />
<embed> element.</h1>
</body>
</html>
The preceding options work well, provided you are using only very short video clips. If your clips will run even 15–30 seconds, however, your visitors may become very impatient waiting for them to download. For long clips—audio or video—you need to look into streaming media.