Zhafur
Zhafur

Reputation: 1624

Gapless sound loop in flash

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?

Upvotes: 0

Views: 4054

Answers (1)

Strille
Strille

Reputation: 5781

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

Upvotes: 2

Related Questions