138 | Chapter 4: AIR Mini-Cookbook
Here is the relevant code snippet:
var soundPath =
new air.URLRequest("app-resource:/sound.mp3");
var s = new air.Sound( );
s.load(soundPath);
s.play( );First, we create aURLRequestthat points to the location of the
MP3 file we will play. In this case, we use an app-resource
URI that references thesound.mp3file contained in the appli-
cation install directory. You can also use any valid URI,
including both file and http URIs:
var soundPath =
new air.URLRequest("app-resource:/sound.mp3");We then create an instance of the Sound class, pass the refer-
ence to the MP3 path, and then call play:
var s = new air.Sound( );
s.load(soundPath);
s.play( );Here is the complete example with a play button:
<html>
<head><script src="AIRAliases.js" />
<script type="text/javascript">function playSound( )
{
var soundPath =
new air.URLRequest("app-resource:/sound.mp3");
var s = new air.Sound( );
s.load(soundPath);
s.play( );
}
</script></head><body>
<input type="button" value="Play" onClick="playSound()">
</body>
</html>