[AS3] PLAY A WAVE FILE FROM LOCAL DISK

June 9, 2011 at 1:34 PM (Actionscript) (, , )

In one of my previous post we saw how to Record sound data with microphone and save it as wave file…but what we didn’t see there was how to open and playback the saved wave file. One of the readers requested for that feature, thought ill post it so that it may help others who might have similar requirement

The issue we need to overcome before acheiving this:

+ We cannot select and play a audio file since fileReference object doesn’t return the file path of the audio file [this will not be an issue if you are developing a AIR application, as you can use ‘File’ object which will return file path]. So our only choice here is to load the audio file as binary data using fileReference objects ‘load’ method.

How are we going to acheive it

+ Convert the wave audio data into raw data and use Sound objects Sample data Api to play back.

What we need?

An API which can convert Wave into raw audio..

There are several apis to choose from

In this demo I’m going to use..
ActionScript 3 OggVorbis Library which is put up @ adobe labs in alchemy example libraries…

The library simplifies our task, it decodes wave into raw data and also takes care of playing back the raw sound data:

1. download the oggvorbis library source code from this adobe labs link:

http://labs.adobe.com/wiki/index.php/Alchemy:Libraries

2.unzip and add the src/com folder to your project folder…

Presenting the necessary code below:

package
{
	import flash.net.FileReference;
	import flash.net.FileFilter;
	import flash.events.Event;
	import flash.utils.ByteArray;
	import flash.events.MouseEvent;
	import flash.events.IOErrorEvent;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.display.Sprite;
	//
	import com.automatastudios.audio.audiodecoder.AudioDecoder;
	import com.automatastudios.audio.audiodecoder.decoders.*;
	//
	public class Main extends Sprite
	{

		private var decoder:AudioDecoder;
		//Create an instance of soundchannle object to control the sound
		private var channel:SoundChannel;
		private var open_file:FileReference=new FileReference();
		//
		public function Main()
		{
			// constructor code
			//
			open_file=new FileReference();
			//Instance of AudioDecoder
			decoder = new AudioDecoder();
			decoder.addEventListener(Event.INIT, onDecoderInit);
			decoder.addEventListener(Event.COMPLETE, onDecoderComplete);
			decoder.addEventListener(IOErrorEvent.IO_ERROR, onDecoderIOError);
			//
			open_file.addEventListener(Event.SELECT,onfileOpenSelect);
			open_file.addEventListener(Event.COMPLETE,onfileOpenLoad);
			//Script for your button which will open the wave file;
			open_lab.mouseEnabled = false;
			open_btn.buttonMode = true;
			open_btn.addEventListener(MouseEvent.CLICK,selectWav);
		}
		private function selectWav(evt:MouseEvent):void
		{
			open_file.browse([new FileFilter('Wave','*.wav')]);
		}
		private function onfileOpenSelect(evt:Event):void
		{
			open_file.load();
		}
		private function onfileOpenLoad(evt:Event):void
		{
			var ba:ByteArray = open_file.data;
			trace(":: Loading Audio Data: ");
			if (channel)
			{
				channel.stop();
			}
			//The library can decode and play wave as well as oggVorbis
			//-!define which decoder you want use here we are using Wave so pass 'WAVDecoder'
			//-!for class parameter
			decoder.load(ba,WAVDecoder);
		}

		private function onDecoderInit(event:Event):void
		{
			//Triggered on sound decoding complete and ready is ready for playback
			trace(":: THE DATA IS READY FOR PLAYBACK: ");
			trace(decoder.audioInfo.toString());
			//You can call the below script either here allowing it to play as soon as you open the file
			//-- or you can call it in different event..like you can add a play button and call it at of the button..
			channel = decoder.play();
		}

		private function onDecoderComplete(event:Event):void
		{
			//Triggered on sound playback complete
			trace( ":: Sound PlayBack Completed!");
		}

		private function onDecoderIOError(event:IOErrorEvent):void
		{
			trace( ":: Error: " + event.text);
		}
		//;
	}
}
Advertisements

Permalink 5 Comments

%d bloggers like this: