Quick Tip: Play External WAV Files in AS3

Quick Tip: Play External WAV Files in AS3

Tutorial Details
  • Difficulty: Beginner
  • Platform: Flash (Flash Player 10)
  • Language: AS3
  • Software required: Works in any Flash development environment
  • Estimated Completion Time: 15 Minutes

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!

  • Jack

    I m not sure why this gets written like this so often. You should be listenin gfor the INIT not COMPLETE. COMPLETE signifies that it has finished loading. The INIT even gets fired when the asset is actually initialized.

    wav.addEventListener(Event.COMPLETE, playWav);

    needs to be

    wav.addEventListener(Event.INIT, playWav);

  • http://blofair.com Technology News Blog

    Very much helpful topic sir,
    i like that and i want to say that i am totally agree with you.

  • viaria

    there are better formats and some of them will be available in the next release. why insist wave file. is it quality.

  • http://www.3rddesign.com 3rddesign

    Thanks for the tip. This is such a very helpful topic for me since I am just a beginner.

  • Sebastian

    Hi, this helpme a lot. I make some kind of playlist, but my problem is how can i stop and then restart this playlist, because the only i find to stop is SoundMixer.stopAll();, but after that, i dont know why i cant play the sounds again. help me please!!

  • Bhargavi

    Hi
    This tutorial is very good.

  • Erwinus

    Not working with large WAV files. Tried it with a 600MB PCM 44100 2 Channel and 16 bits encoding but does not play. Flash project crashes also. You need to restart flash to reopen the project.

  • Siilk

    Thanks, Carlos! That worked great for me. Oh, BTW, Jack is right, Event.COMPLETE is the way to go.

  • http://morpheus.micc.unifi.it/damico/ Gianpaolo D’Amico

    Hi, nice tutorial, thank you very much.

    I’m working in Flash Builder in an AS3 project for AIR and for me:

    Event.COMPLETE is the good one, the INIT event does not work properly

  • Fernando

    Nice post, I wonder how do I click again when the sound stops. thank you

  • Ramcerva

    has more delay than using mp3 files, is that normal?