MP3 to SWF converter in Actionscript 3
February 13th, 2009
A while ago I was playing with SWF generation within Flash itself.
I read the Adobe SWF specs and now I'm able to generate SWF's from FLV's and MP3's or both.
A simple Flash 10 example, haven't tested all possible MP3 rates so if someone encounters a bug, please let me know.
(Just found out that some MP3's with embedded art-work won't work... added to my TODO's).
Code snippet which writes the MP3 data to a SWF bytearray:
while(mp3.audioFrames.length > currentAudioFrame) { var needSamples:int = 0.001 * ((1000/(__frameRate))*i) * audioRate; var startSamples:int = currentSamples; var audioData:ByteArray = new ByteArray(); while(mp3.audioFrames.length > currentAudioFrame && currentSamples< needSamples) { var aframe:AudioFrame = AudioFrame(mp3.audioFrames[currentAudioFrame]); audioData.writeBytes(aframe.frameData); currentSamples = (currentAudioFrame+1) * (576*2); currentAudioFrame++; } var samples:int = currentSamples - startSamples; var seek:int = startSamples ? needSamples - startSamples : 0; var af:AudioFrame = mp3.audioFrames[i] as AudioFrame; _swfBytes.writeShort(createHeader(SWFTags.SOUNDSTREAMBLOCK,63)); _swfBytes.writeInt(audioData.length+4); _swfBytes.writeShort(samples); _swfBytes.writeShort(seek); _swfBytes.writeBytes(audioData); _swfBytes.writeShort(createHeader(SWFTags.PLACE_OBJECT2,3)); //Place frame data _swfBytes.writeByte(1); _swfBytes.writeShort(1); //depth _swfBytes.writeShort(createHeader(SWFTags.SHOWFRAME,0)); i++; }
This way I write all SWF headers the correct way and not like most other code out there which uses a precompiled SWF and replace the sound object.
Very cool. With this technique can you load the swf and access the data as a Sound object? How would you play the mp3? Is there a way to generate a Sound instance and load the data into it outside of a URLRequest?
I was using the precompiled SWF / Sound object technique you mentioned above until I decided to use fvorbis instead for my audio needs.
But I’m interested in how you accomplish accessing the mp3 (AudioFrame in particular) and writing it out to generate a proper swf for playback.
I guess I’ll have to look link the SWF specs sometime.
Thanks for the post. I hope to see more on this!
-Don Q.
Well the audioframe is my own class. It loads a mp3 into a bytearray. Then I analyze the mp3 and pick out all frames. When you create a SWF you can add mp3 frames to a sound object like it’s an embedded sound.
When I tidy up some of the code I will post some more.
Cool. Thanks for the reply.
-Don Q.
Do you know if there is a way to do the opposite : to create a mp3 file from a ByteArray, build with the SoundMixer class ? My goal is to generate a FLV file, from several external ressources (videos, sounds, graphics…).
Haha! I found a bug
My MP3 Encoder creates MP3’s that can’t be loaded this way. I’ll have to take a look at it.
very cool!
i know you can get an flv in a byteArray when you load using fileReference.data so you recon its possible to do the same with an flv?
I know it’s possible with a FLV, because I did it already.
The main thing is you have to parse the FLV to single FLV frames and put them in a SWF. Both specifications are open and downloadable at the adobe site.
@Jérémie
Hi Jérémie,
I was wandering if you have found a way to create an MP3 from a ByteArray? I’ve been trying for more a day now but with little success.
Thanks!
Atanas.
Nice work man you should develop this in to a full featured library