views:

367

answers:

1

I have a legacy file format that contains sounds embedded in it (in various encodings). I would like to be able to play these sounds in Flash (Air?) by reading the sound bytes out of the file and instantiating a Sound object with them.

If the sound is unencoded (e.g., raw pcm), I've found that I can use the new flex 4 SampleDataEvent.SAMPLE_DATA event to play the sound.

However, if the sound is encoded (e.g., mp3), then I'm at a loss. The sound expected by SampleDataEvent.SAMPLE_DATA has to be raw pcm. From what I've seen, encoded Sounds can only be instantiated by [Embed]ing them, or by using a URLRequest with Sound.load().

Surely there's a third way? AMF or e4x?

+3  A: 

There are really only two routes for you to go. The first is to write a decoder in ActionScript. You may be able to use Alchemy to port over some C/C++ code to make this job significantly easier (and possibly more performant). This is exactly how I got Ogg Vorbis playback to work with Flash.

The other option is to dynamically create a valid SWF inside of a ByteArray. That SWF could contain an embedded sound object that was made up of your sound data. A number of folks have pulled off similar hacks in the past before Flash Player 10 was available. I believe you can find a good place to start in Andre Michelle's and Joa Ebert's PopForge codebase.

Branden Hall
I tried the technique of dynamically creating a sound-asset SWF and it works. What a great idea.For anyone else trying this, I recommend these tools for getting a grip on the SWF format: SWF Spec (http://tinyurl.com/c6a2g4), 010 Hex Editor (http://tinyurl.com/d5gjad), SWF Hex Template (http://tinyurl.com/n5vk4z), and SWF Tools (http://tinyurl.com/38edhl)
paleozogt