Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL

To play sounds

In your project’s primary HTML file, create the necessary objects (Script 14.1):

var url = new air.URLRequest('app:/sounds/bell.mp3');
var sound = new air.Sound();

The two requisite objects are defined here. The first is of type URLRequest and points to the bell.mp3 sound found within the application’s sounds directory (Figure 14.1).

Figure 14.1. This image of Aptana Studio’s project directory shows the sound file required by the program.

Create an anonymous function that loads the sound:

window.onload = function() {

After the application (technically, the window) has loaded, this anonymous function will be called. It loads the sound file so that it’s ready to play when appropriate.

Create a function that plays the sound:

function playSound() {;

Invoking the Sound object’s play() method is all that’s required.

Within the body, create a button that invokes the playSound() function:

<button onclick="playSound();">Play the Sound</button>

Place the bell.mp3 file in your project’s sounds directory.

You can download this file from the book’s supporting Web site (, see the Downloads page). Alternatively, you can use your own sound file. Just make sure that it’s relatively short—not an entire song, for example—and change the code in the script to match that file’s name.

Save, test, debug, and run the completed application (Figure 14.2).

Figure 14.2. The application, which is more impressive aurally than it is visually.

✓ Tips

  • The ability to work with sounds found within a SWF file is defined in the SoundMixer class.

  • AIR applications can also access sound through a computer’s microphone via the Microphone class. See the online documentation for more information.


You are currently reading a PREVIEW of this book.


Get instant access to over $1 million worth of books and videos.


Start a Free 10-Day Trial

  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint