|
The Hills are Alive...
Nonstop Rock
|
<HTML>
<BODY>
<BGSOUND SRC="snd1.mid">
Welcome to the page of sound.
</BODY>
</HTML>
This page plays a MIDI file once, as soon as the page is loaded. Using the LOOP parameter, you can make the sound play more often. For example, the following will play the MIDI track three times:
<HTML>
<BODY>
<BGSOUND SRC="snd1.mid" LOOP=3>
Welcome to the page of sound.
</BODY>
</HTML>
To make the track play the entire time that the page is visible, set LOOP to INFINITE. Of course, doing so can certainly cause anyone looking at your page to go nuts.
Music on Demand
It's fun to play background music, but playing sounds in response to user actions is far more interesting. For example, you can play a sound when the user clicks on a link or mouses over an image hot spot. There are two basic ways to play music in response to events: you can use the <BGSOUND> tag or the ActiveMovie control.
I'll begin with the <BGSOUND> tag, which is similar to using it for adding background music. I'll give the <BGSOUND> an ID, and leave the SRC blank. Then I'll set the SRC parameter through a script run in response to an event. For example, the following page will play a sound when you mouse over certain words on the page:
<HTML>
<HEAD>
<BGSOUND id=snd1>
</HEAD>
<BODY>
The page of <FONT COLOR=blue onmouseover="snd1.src='snd1.wav'">sound</FONT>
will play <FONT COLOR=blue onmouseover="snd1.src='snd2.wav'">noises</FONT>
for you.
</BODY>
</HTML>
In the browser, the words with sounds attached are highlighted in blue so that you can see them more easily. When you mouse over a word, such as "sound," the script sets the SRC for the <BGSOUND> tag. This causes the sound to play.
<HTML>
<HEAD>
<BGSOUND id=snd1>
<BGSOUND SRC="snd1.mid">
</HEAD>
<BODY>
The page of <FONT COLOR=blue
onmouseover="snd1.src='snd1.wav'">sound</FONT> will play <FONT COLOR=blue
onmouseover="snd1.src='snd2.wav'">noises</FONT> for you.
</BODY>
</HTML>
If you want to make sure that a sound is available as soon as the user mouses over it, you can preload it into the cache. A simple way is to add a tag for the sound file and set its volume to a level that can't be heard, like -10000.
Active Sounds with ActiveMovie
<BGSOUND> is simple, but it won't work for all your needs. There are three disadvantages to using <BGSOUND>. First, you have no way of knowing when a sound has downloaded. If you need to play a fairly large sound and you want to synchronize it with some other actions, such as moving elements across the page, you won't be able to guarantee that the sound will start when the element starts moving. Second, there can be a pause between loops. If you have a small sound that you loop frequentlysay for a drum trackyou might get an annoying hiccup between repetitions. Third, and perhaps most significantly, you can't stop the sound once you start it. This is particularly troublesome when you are playing a looping sound. Although you can change the SRC to "", it will stop when it feels like stopping, not when you tell it to. One way to work around this is to cut the volume as soon as you want the sound to stop. It will keep on playing, but you won't hear it.
ActiveMovie, on the other hand, provides very little
pause between loops. It will load a WAV file and set a property value as soon as the file is ready to play. Another property tracks the status of the sound, including indicating when the file has completed playing. Also, you can easily stop and restart sounds.
ActiveMovie has its downsides too. It is much more
complex than <BGSOUND>, and you need to include an ActiveX® control in your page. ActiveMovie doesn't stream, at least not with Internet Explorer 4.0, so you'll need to wait until the entire sound file is downloaded before it will play. Also, ActiveMovie doesn't read from the cache, so even if you've already downloaded a page, the next time you visit it you'll need to download the entire set of sounds again.
You need to figure out which sound solution makes sense for your needs. If you need to provide exact coordination of sound with an animation, or you need to start and stop sounds, use ActiveMovie. If streaming and reading from the cache are more important, use <BGSOUND>.
To use the ActiveMovie control, you first need to add the control to your page:
|
Then you can play the sound by calling the Run method on the control. Make sure that there is a sound card in the client machine; if there is no sound card, you will get an unfriendly script error message. Next, you need to make sure that enough of the sound has downloaded so that it can be played. Once again, if you don't check, you will get an obscure error message. You can see how to perform these checks with the following code, which will play a sound once it is downloaded: |
|
The only trick is that detecting the sound card doesn't work if you are running NetShow. Figure 1 shows a complete page that uses ActiveMovie to play a sound.
Is it Safe Yet?
|