MP3 to SWF converter in Actionscript 3
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.
