Flex Files: Sound Exploration – Loop an Mp3 with a ByteArray?

Flex Files: Sound Exploration – Loop an Mp3 with a ByteArray?

After reading through all of the documentation on loading and embedding sounds in Flex Framework swf file, I have come to the conclusion that Adobe has been and continues to have its collective head in the sand when it comes to managing sound in actionscript. When I first started using CS3, I was appalled by the lack of information on using library embedded sounds. I finally uncovered the right docs after numerous fruitless searches and created this basic tutorial on controlling sounds embedded in the library. When searching the message boards on the subject I found numerous newbee response posts from people chastising others when they claimed to have problems looping an imported mp3 properly. Most people have absolutely no idea that the current mp3 spec was not created with looping in mind, so when you play a mp3 file loaded in or embedded in the library, you will ALWAYS hear a little silence at the end of the sound before it starts to play again. That doesn’t happen with .wav file imported into the library and exported to compress with mp3 compression. This only happens when to try to loop a file saved as a mp3 and loaded in or embedded in the library. I had to shake my head after reading far too many “know-it-all” posts from 12 year olds claiming “DUDE, GET A BETTER MP3 ENCODER, Of COURSE THEY LOOP”. I used to write into those boards and set the record straight, but I decided to end that fight and concentrate on fixing the problem. The problem has been in Flash since its inception, but NOTHING has been done to fix the it. Simply allowing the user to load in an aiff, wav or au file at run time would have solved the problem, but nothing was ever added to do do this. Also, some how the onSoundComplete event NEVER FIRES in AS3. What a joke. After numerous complaints from users, the problem was not fixed in FP9. Let’s hope it has been in FP10.

Back to the mp3 looping problem. My first attempt to fix the problem was way back in Flash 8. I created this tutorial on the MP3 spec and how to loop a loaded in .mp3 file by skipping over the leader and follower blank space. It works fine and I used it in a couple Flash 8 apps. I haven’t needed it so far to target the Flash 9 player, but now that I have researched sounds in Flex, I might have to. This is where Adobe has hid it head in the sand, and continues to. You CANNOT EMBED wav, aiff, au or any other sound file BUT mp3 in your Flex apps. YIKES! That was a huge blow when I found out. Now, there certainly are some ways around the problem. One is to embed the wav files into a swf in Flash and then load that swf into Flex. Sure, that will work, but I have been trying to rid myself of the need for the Flash IDE in my game development. I need a better way. I could re-write the AS2 tutorial above for Flex and create a class to control mp3 files by poling while playing and moving the sound play head back to the start when needed. That certainly will work too.

I hate to reuse older ideas if there is a better way though, so my next exploration will entail embedding the .mp3 (or loading it in), but using the ByteArray class to copy the BYTES of sound from the file to an array of bytes that excludes the leader and follower information. Then, I can just play the sound and not worry about skipping over the leader and follower information because it won’t exist. I figure this MUST be possible because the new FP10 spec allows the user to load a file from his/her computer as a ByteArray. Actionscript will need to be used to turn that file into an image or sound or what ever by casting it as the appropriate type. I should be able to do that exact same thing with my embedded or loaded in mp3 files.

Here is how the process might work:

Extend the mx.core.ByteArrayAsset class by Embedding and asset with the appropriate mime type:
[Embed(source=”music.mp3″, mimeType=”application/octet-stream”)]
Private var Mp3BinaryArrayAsset:Class;

I have no idea where to go from there, but I will be spending some time this week on it.

Here in the USA, most people have the Thanksgiving holiday this week on Thursday and Friday. I hope to have this figured out and up as a tutorial by Friday.
First things first, tonight is the series finale of The Shield, one of the best shows in the history of TV.

Leave a Reply