|
Free Open Book
Sams Teach Yourself JavaScript in 24 Hours |
Playing Sounds with JavaScriptAlthough the W3C DOM has made advanced effects and applications possible in JavaScript in a painless, cross-browser fashion, no standard has emerged to do the same for JavaScript's sound support. There are a few ways of making JavaScript play sounds, and none of them work consistently in all browsers all of the time. Nonetheless, with a bit of effort, you can play sounds in most browsers. By the Way Because sound support in browsers is inconsistent, there's no guarantee your sounds will work for everyone. Be sure any sound you use in JavaScript applications is optional and that the script still works even on browsers that won't play the sounds. Sound FormatsThere are a wide variety of sound formats, usually identified by their file extensions. The following are some of the most common sound formats on the Web:
Any of these formats can be supported by most browsers, but unfortunately there is no format that is universally supported. If you're hoping as many visitors as possible will be able to hear your sounds, the best choice is .au if you're using standard audio plug-ins, or .mp3 if you're using Flash. Sound-Playing Plug-InsBrowsers almost always require a plug-in to play sounds. Fortunately, sound plug-ins are widely used and many of your site's visitors already have one or more of them installed. Here are the most common sound-playing plug-ins:
Embedding SoundsThe following is a simple example of an <embed> tag to embed a sound in a page: <embed id="note_c1" src="c1.au" width="0" height="0" autostart="false" enablejavascript="true"/> This example works with the most common sound plug-ins. It specifies a source filename for the sound file (c1.au) and autostart="false" to prevent the sound from playing when the page loads. The enablejavascript parameter is required by some plug-ins to allow scripting. The width and height parameters set the size of the embedded player. If they are not zero, the player will be visible with Play, Pause, and Stop buttons. Setting them to zero hides the player, useful when you intend to control it strictly with JavaScript. (A hidden parameter is supposed to hide the player, but this causes sounds not to play in some browsers.) Controlling Sounds with JavaScriptAfter you've embedded a soundassuming a browser plug-in supports ityou can use the following methods of the sound object to control the sound:
Watch Out Depending on the audio plug-in in use, the methods supported might be different. Always use TRy and catch when attempting to control sounds to avoid errors. Detecting Sound SupportBecause you can't count on sounds being supported by all browsers, it's a good practice to use try and catch to test the statements and display a message (or take another appropriate action) if sounds are not supported: try {
sound.DoPlay();
} catch (e) {
try {
sound.Play();
} catch (e) {
alert("No sound support.");
}
}This code first tries RealPlayer's DoPlay() method. If that doesn't work, it tries the Play() method. If neither approach works, it displays an error message. By the Way The TRy and catch keywords are used to test a risky statement, find out whether it works, and suppress the browser's usual error messages. See Hour 16, "Debugging JavaScript Applications" for more information. Using FlashIf you are relying on sounds for an application, you might want to consider using Flash. You can create a simple Flash object that loads sound files and allows JavaScript to play them. This gives you scriptable sounds using one consistent plug-in that works on most platforms. Scott Schiller's SoundManager provides an easy way to use Flash sounds from JavaScript. SoundManager uses a Flash object to play MP3-formatted sounds you specify in an XML file. After you've created the XML file and included SoundManager using a <script> tag, you can use its methods to control the sounds. More information and the download for SoundManager are available at http://www.schillmania.com/projects/soundmanager/. |
Main Menu |
| 500 Juegos Gratis | 500 Giochi Gratis | 500 Jeux Gratuits | 500 Jogos Gratis | 500 Kostenlose Spiele |