You probably noticed if you've tried to make any looping song in your Flash game that there's a gap between each loop.
[ Embed ( source = "song.mp3" ) ] private var Song:Class;
var music:Sound = new Song();
music.play(0, int.MAX_VALUE);
After some digging around, I noticed when I traced the length of the music it was actually longer then the actual song! The weird thing is, it's not even a nice number:
trace(music.length);
Converting this to milliseconds we see that there's around an 83 millisecond gap added to the length of the song for some reason. After reading around the net, I came to understand this is because of
how mp3 encoders/decoders work. There is apparently a
solution if you have Adobe CS3 but I don't have $670 to buy it so I'm using Flash Develop. Here is how I got around it in plain old actionscript 3:
var timer:Timer;
var channel:SoundChannel;
timer = new Timer(music.length-83);
timer.addEventListener("timer", function (e:Event):void {
channel= music.play();
});
timer.start();
var initialTimer:Timer = new Timer(0, 1);
initialTimer.addEventListener("timer", function (e:Event):void {
channel= music.play();
initialTimer = null;
});
initialTimer.start();
To Stop the song you will also need to kill the timer:
timer.stop();
channel.stop();
Not exactly elegant but it works and for you CS3 users, it lets you actually embed the mp3 instead of a wav version of it... unless you have a slow computer, then the time between a frame can become too long for the timer to kick in often enough and this causes the gap to happen again.
There is another work around that I've heard solves the gap problem. I am trying it on Zening music, but they have not yet implemented it so still not 100% sure. If you create the mp3 loop in Flash, then it should loop seamlessly in the game. The loops I made loop seamlessly when played in my browser so that's a good sign. I'l let you know if it works.
g