Sams Teach Yourself JavaScript in 24 Hours Free Open Book

Sams Teach Yourself JavaScript in 24 Hours

Previous Page
Next Page

Playing Sounds with JavaScript

Although 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 Formats

There 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:

  • .au (Audio Unit) The earliest sound format supported by browsers, and still the most widely supported. Some browsers have built-in support for this format. In Firefox, the QuickTime plug-in supports .au files.

  • .wav The standard Windows sound format (usually played by Media Player on Windows machines).

  • .mp3 A compressed format for larger files, such as music. MP3 plug-ins are not included with most browsers, but are often installed by users.

  • .mid (MIDI) Rather than audio, MIDI files store note information to re-create a song using a standard set of instruments. Most computers support MIDI music, although a browser plug-in might be required.

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-Ins

Browsers 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:

  • QuickTime Apple's sound and video player, installed by default on Macintosh systems. QuickTime plug-ins are also available for Internet Explorer for Windows and for Firefox on Windows and Macintosh.

  • Windows Media Player Microsoft's sound and video player, installed by default on Windows systems.

  • RealPlayer A popular third-party plug-in for playing music and video, available from http://www.real.com.

  • Flash Although the Flash plug-in doesn't play standard embedded sounds, Flash animations and movies can play sounds, as you'll learn later in this section.

Embedding Sounds

The 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 JavaScript

After you've embedded a soundassuming a browser plug-in supports ityou can use the following methods of the sound object to control the sound:

  • Play() or DoPlay() Starts playing the sound, and stops when the sound is finished. DoPlay() is supported by RealPlayer, and Play() is supported by most other sound plug-ins.

  • Stop() Stops the currently playing sound.

  • Rewind() Restarts the current sound at the beginning.

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 Support

Because 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 Flash

If 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/.

Previous Page
Next Page
Index: [SYMBOL][A][B][C][D][E][F][G][H][I][J][K][L][M][N][O][P][Q][R][S][T][U][V][W][X][Y]


     Main Menu
Sams Teach Yourself JavaScript in 24 Hours
Table of Contents
Copyright
About the Author
Acknowledgments
Part I: Introducing the Concept of Web scripting and the JavaScript Language
Part II: Learning JavaScript Basics
Part III: Learning More About the DOM
Part IV: Working with Advanced JavaScript Features
Part V: Building Multimedia Applications with JavaScript
Hour 19. Using Graphics and Animation
Hour 20. Working with Sound and Plug-Ins
Introducing Plug-Ins
JavaScript and Flash
Playing Sounds with JavaScript
Testing Sounds in JavaScript
Summary
Q&A
Quiz Questions
Quiz Answers
Exercises
Part VI: Creating Complex Scripts
Part VII: Appendixes
Index


More Books
PHP Hacks
Processing Xml With Java - A Guide To Sax, Dom, Jdom, Jaxp, And Trax
The Koran (Holy Qur'an)
Macromedia Flash 8 Bible
Search Engine Optimization for Dummies
YouTube Traffic
PHP 5 for Dummies
Harry Potter and The Chamber of Secrets
Harry Potter and the Sorcerer's Stone
The Pilgrim's Progress
Wireless Hacks
Flash Hacks. 100 Industrial-Strength Tips & Tools
PayPal Hacks. 100 Industrial-Strength Tips and Tools
Amazon Hacks
Pdf Hacks
The Da Vinci Code
Google Hacks
The Holy Bible
Windows XP For Dummies
Harry Potter and the Half-Blood Prince
Seo Book
Upgrading and Repairing Networks
Macromedia Dreamweaver 8 UNLEASHED
Windows XP Annoyances
Windows XP Hacks
Microsoft Windows XP Power Toolkit
Teach Yourself MS Office In 24Hours
iPod & iTunes Missing Manual
PC Hacks 100 Industrial-Strength Tips and Tools
PC Overclocking, Optimization, and Tuning - 2th Edition
PC Hardware In A Nutshell 3rd Edition
PC Hardware in a Nutshell, 2nd Edition
Upgrading and Repairing PCs
Google for Dummies
MySQL Cookbook
Teach Yourself Macromedia Flash 8 In 24 Hours
PHP CookBook
Sams Teach Yourself JavaScript in 24 Hours
PHP5 Manual
Free Games Paper Airplanes
500 Juegos Gratis 500 Giochi Gratis 500 Jeux Gratuits 500 Jogos Gratis 500 Kostenlose Spiele