0
votes

I made a function which loops the mp3 infinitely until I stop it:

private function loopSound(a:Class, lead:Number, trail:Number) {
            var b = new a();
            var sChannel = new SoundChannel();
            var timer:Timer = new Timer(b.length - lead - trail, 1);
            sChannel = b.play(lead);
            timer.addEventListener(TimerEvent.TIMER_COMPLETE, function r(e:TimerEvent) { loopSound(a, lead, trail); } );
            timer.start();
        }

The problem with this function is that it doesn't loop correctly. Though my calculating is correct(from my view), flash doesn't calculate the positions correctly. Any idea how to achieve flawless mp3 loop?

  • lead = gap at the beginning in ms (27.375ms in my case)
  • trail = gap at the end in ms (20.278ms in my case)
1
how far off is it? Is it just that .375ms? - nycynik
It's always changing. At the first loop, it's great, but at the second, it seems that the timer fires earlier. If I trace the timer's delay, it shows the same number always. Then what's the problem? - Zhafur

1 Answers

2
votes

Using the TimerEvent to loop will not be accurate enough because the way events are triggered in the Flash Player.

The easiest way to play a seamless "infinite" loop is to:

  1. Have the sound in .wav format perfectly trimmed from silence at the beginning and end
  2. Import it to Flash's library and export it for ActionScript (as mp3 for low file size)
  3. Simply create a new instance of the sound and play it using Sound.play() and specify how many times to loop as the second argument (a really large integer pretty much makes it "infinite" for most purposes).

There are more powerful solutions as well that might be of interest: Prefered method for looping sound flash as3