Hidden Marvels in Flash: Gapless MP3s

– taterboy | September 11th, 2010

Filed under: Audio, Flash, Flex, Games, Tutorials

Gapless MP3s

Ever try to loop an mp3 file in Flash or Flex? It can be done, but there’s normally an annoying gap every time the audio loops. This gap is just a part of mp3 compression and there’s nothing that can be done about it.

Example:
Take an aiff or wave file, open it in your favorite sound editor and make a loop (remove all gaps at the beginning and end of the file). Now save the file, first as an aif or wave, then as an mp3. Now close the files and reopen them. Notice the mp3 file has a gap at the beginning and end of the waveform, the wave and aiff files do not.



The Solution: Flash
Our project requires an audio loop, with it living on the web, needs compressed audio files. One of Flash’s hidden features is that of an audio compressor, namely mp3 compressor. The only difference is when Flash compresses audio from a non-mp3 source, Flash Player plays the compressed audio without a gap at the beginning and end, in a perfect loop. In this test, compressed at 128kbps/mono, the swf is about 1/6 the size of the original aif file.

Uses:
1. Take an aif file, import it to the Flash timeline, set it to stream and spread out the frames to match the length of the audio. Go to: File > Publish Settings > Flash to set your compression settings and then publish. Now you have a gapless audio loop in Flash.





You can also use actionScript to play and loop the audio.

?View Code ACTIONSCRIPT
var loop:Sound = new BackroadsBanjo() as Sound;
loop.play(0,999);

note: If you change the .swf extension of a timeline based swf, with mp3 compressed audio, to .mp3; it will act as an mp3 and play in many audio players. If you open the file in an audio editor it looks exactly like an mp3 file including the dreaded gaps.

note: Gapless mp3 compressed audio only works in the Flash Player.

2. Import your aif or wav file into Flash, double-click on the audio in the Flash project library.



Select Export For ActionScript and add a cool name as the class name.



Click OK to the “definition for this class could not be found” alert dialog window.



In Publish Settings under the Flash tab, check Export SWC and set your compression settings.



Now publish and place that swc in the libs folder of your Flex project. Now you have a gapless mp3 object for use in your Flex project.

?View Code ACTIONSCRIPT
var loop:Sound = new BackroadsBanjo() as Sound;
loop.play(0,999);

Technorati Tags: , , , , , , , ,

| No Comments » | facebook:

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment

*