ColdFusion in Context: Short Sound Alerts

Suppose you want to hear a short sound in response to an action. How do you readily do this with both major browser families? Here's a working example.

Get Sounds

Look on your own computer. For this tip, find files ending in .wav and files ending in .mid (or .midi).

Make a Sound Producer

Put this code in sound.cfm. You'll include this file when you want a sound. It will look for the variable Soundsrc: the url to the sound file. (This is just the filename if the sound is in the same directory as the code.) If Soundsrc is defined, and if the corresponding file exists, then try to play the file two different ways.

If this is an old IE browser (perhaps 3...), the bgsound tag will try to function. If this is a current IE browser (version 5...) or a Netscape browser that isn't brain dead version 6 - sorry, I don't how else to characterize that particular browser - the embed tag will try to function. The bgsound tag hides the audio controls by default. The embed tag is more flexible and therefore has to be told to start by itself and to hide the controls. Because some browsers understand both embed/noembed and bgsound, the bgsound tag has been nested inside a noembed tag to avoid redundancy.

<cfif isDefined("Soundsrc")>
  <cfif fileExists(expandPath(#Soundsrc#))>
    <cfoutput>
    <embed src="#Soundsrc#" autostart="true" hidden="true">
    <noembed>
    <bgsound src="#Soundsrc#">
    </noembed>
    </embed>
    </cfoutput>
  </cfif>
</cfif>

Make a Menu

Put this code in soundtest.cfm. If if finds that you've defined src in the URL, it sets Soundsrc to the value of that variable and includes sound.cfm to try to play it. In any event, it lists some links for you to try. Each link points back to this page and includes the path to the sound as a url variable named "src". (It could be called anything you wish.)

<cfif isDefined("url.src")>
  <cfset Soundsrc=url.src>
  <cfinclude template="sound.cfm">
</cfif>

Listen to these...
<a href="soundtest.cfm?src=kapow.wav">WAR</a>
<a href="soundtest.cfm?src=moo.wav">FARM</a>
<a href="soundtest.cfm?src=phone.wav">PHONE</a>
<a href="soundtest.cfm?src=music.mid">MUSIC</a>

Check it Out

Browse soundtest.cfm. Whether this works depends on the last program you downloaded to listen to sound. If you start with a newly configured player program (perhaps Windows player, Real Audio, etc.), the browser may work without further adjustment. However, if you subsequently downloaded a different player, it may have dropped the first player's access to these file extensions without identifying itself to the browser as the program of choice. You may get no sound. You may get a complaint followed by sound. The browser may even crash. (Netscape 6 crashed.)

I had the best success initially with .wav files. You may have to adjust Netscape's list of applications that it uses for various file types. If multiple plugins support the media type you want to use - you'll probably find that Microsoft applications are listed for MIDI files multiple times - you may have to remove some of those references until you get something that works. Netscape's flexibility works against you when trying to pick media formats that multiple copies of the same Netscape version can hear without having to make individual adjustments of this nature. On one computer I tested with, I wound up removing and reinstalling my Netscape browser and manually changing the settings in Windows Player to get the Netscape browser to play sounds. As I wrestled, solutions that let me play .wav files wouldn't support .midi and solutions that let me play .midi files wouldn't let me play .wav files. I know it's possible to get both at once, but it isn't easy.

Because it is quite likely that the user's computer is not configured properly - one of mine still isn't - I don't recommend this approach for sound to be heard by users. Users typically spend their time configuring their boxes to play streaming audio, not short alert noises. The situation with short sounds is similar to the situation with the mailto tag; you don't use them if you have an alternative.

So given that cross-browser sound takes manual adjustment, what will you do with short sounds? I find them most useful on the administrative side. If you have a window open that refreshes itself periodically to monitor a process, you can let it give you an audible alert when something goes wrong. Use short sounds, and tell us what they do for you. If you find a better way to provide sound to Netscape users, we need to know that, too.

By the way, if one of the sounds you're trying out runs longer than you'd like, you don't have to close browsers to get rid of it. Just click on a different sound; it will replace the current sound immediately. =Marty=