This is a follow up to my previous post about mixing wav files with Actionscript. I couldn`t sleep tonight so I have hacked together some classes that make it easy to load and play wave files at runtime with Actionscript 3.0.
You know that it`s not natively possible to load and play wave files at runtime. The trick is to load the wave file as a ByteArray (e.g. with Loader, FileStream, URLStream…) and than construct a swf file in memory that has the wave bytes embedded. First i created a swf file in memory that has the wave sound embeded and played it back with a StartStream-Tag. I did not liked that method because it does not give you good control over the playback of your sound. So the solution was to create a SWF file that defines a sound that is linked to a class so you can access the Sound Object directly. For those interested in details here is how the SWF file that i create in memory looks like:
Here is a overview of the classes you need if you want to load a wave at runtime:
What the WaveMix class is for will be revealed in the next post đ
In order to load a wave sound these are the basic steps:
// Create a new Wave Reader
waveReader = new WaveReader();
waveReader.read(bytes);
// Create a wave SWF
var waveSwf:WaveSWF = new WaveSWF(waveReader);
waveSwf.addEventListener(Event.COMPLETE, onWaveReady);
private function onWaveReady(evt:Event):void{
sound = WaveSWF(evt.target).sound;
channel = sound.play(0,0);
}
Now you can use all methods of the Sound Object to work with your sound.
All this is still a bit buggy (all hacked together this night :-). So use it at your own risk.
I`ve prepared a little example in AIR that let`s you load a wave sound. The Wave has to be a PCM encoded (uncompressed) wave file.
Download AIR Example
Download Flex Project (Source)
Let me know what you think đ
This was a pretty long night đ so in the next post i`ll add something to this (reading waveforms + mixing sounds)
Benz
Leave a Reply