Advertisement
Media

Quick Tip: Play External WAV Files in AS3

by

By default, Flash can play WAV files when they're located in the Library, but not when loading external files. Read through this Quick Tip to learn how to play them.


Step 1: Brief Overview

play wav files in Flash with AS3

We'll use common methods to request and load the desired WAV file, and use a fantastic third party class to play the loaded file.


Step 2: AS3WavSound

play wav files in Flash with AS3

AS3WavSound (AWS) is an excellent ActionScript class that can play externally loaded wave files, go to its Google Code page and get the source code.

Now this class wasn't exactly created for external wav playback; here is a short description from the author:

The Flex SDK does not natively support playing (embedded) .wav files. Thus far developers worked around this using ugly hacks (generating swf bytedata to trick the Flash Player). Not anymore. AWS in the slimmest sense simply is a single as3 class. It extends the generic Sound class in Flash and adds support for playing back WAVE data. You don't need this sound class if you are working with the Flash IDE or Flex Builder, as they convert .wav data directly to Sound objects. The open source SDK compiler however, does not support this feature. But it does now!

The Flash IDE does indeed convert the wav data to Sound objects, but only for embedded files (imported to the Library); if you want to play an external wav file, use the following method...


Step 3: Usage

Prepare a new ActionScript class and write the following code:

package 
{
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.events.Event;
	import flash.utils.ByteArray;
	import flash.media.Sound;
	import org.as3wavsound.WavSound;
	import org.as3wavsound.WavSoundChannel;

	public final class Main extends Sprite
	{
		public final function Main():void
		{
			playB.addEventListener(MouseEvent.MOUSE_UP, loadWav);
		}
		
		private final function loadWav(e:MouseEvent):void
		{
			var urlRequest:URLRequest = new URLRequest('Phone.wav');
			var wav:URLLoader = new URLLoader();
			wav.dataFormat = 'binary';
			wav.load(urlRequest);
			wav.addEventListener(Event.COMPLETE, playWav);
		}

		private final function playWav(e:Event):void
		{
			var tts:WavSound = new WavSound(e.target.data as ByteArray);
			tts.play();
		}
	}
}

This is basic code for loading an external file in AS3, the URLLoader class uses the URLRequest to determine the location of the file to be loaded (which is loaded as binary data) and when finished, the WavSound class is used to play the sound.


Step 4: Example

Check out the demo for a working example using the code.


Step 5: About WAV Files

Keep in mind that this class won't play every wave sound: the file must have a sample rate of 44100, 22050, or 11025 Hz, and bitrate sample data of 8 or 16.


Conclusion

Use this class to play your external WAV files.

I hope you liked this tutorial, thank you for reading!

Related Posts