Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you loop a sound in flash AS3 when it ends?

What AS3 code is used to loop a sound using AS3?

like image 709
Noah R Avatar asked Mar 26 '11 16:03

Noah R


5 Answers

This won't give you perfect, gapless playback but it will cause the sound to loop.

var sound:Sound = new Sound();
var soundChannel:SoundChannel;

sound.addEventListener(Event.COMPLETE, onSoundLoadComplete);

sound.load("yourmp3.mp3");


// we wait until the sound finishes loading and then play it, storing the
// soundchannel so that we can hear when it "completes".
function onSoundLoadComplete(e:Event):void{
    sound.removeEventListener(Event.COMPLETE, onSoundLoadComplete);
    soundChannel = sound.play();
    soundChannel.addEventListener(Event.SOUND_COMPLETE, onSoundChannelSoundComplete);
}

//  this is called when the sound channel completes.
function onSoundChannelSoundComplete(e:Event):void{
    e.currentTarget.removeEventListener(Event.SOUND_COMPLETE, onSoundChannelSoundComplete);
    soundChannel = sound.play();
}

If you want the sound to loop many times with a flawless, gapless playback, you can call

sound.play(0, 9999); // 9999 means to loop 9999 times

But you still would need to set up a soundcomplete listener if you want infinite playback after the 9999th play. The problem with this way of doing things is if you have to pause/restart the sound. This will create a soundChannel whose duration is 9999 times longer than the actual sound file's duration, and calling play(duration) when duration is longer than the sound's length causes a horrible crash.

like image 136
scriptocalypse Avatar answered Nov 06 '22 13:11

scriptocalypse


var sound:Sound = whateverSoundYouNeedToPlay;
function playSound():void
{
    var channel:SoundChannel = sound.play();
    channel.addEventListener(Event.SOUND_COMPLETE, onComplete);
}

function onComplete(event:Event):void
{
    SoundChannel(event.target).removeEventListener(event.type, onComplete);
    playSound();
}
like image 44
frankhermes Avatar answered Nov 06 '22 11:11

frankhermes


import flash.media.Sound;
import flash.media.SoundChannel;

var mySound:Sound = new Bgm(); //Bgm() is the class of the internal sound which can be done in the library panel. 

playSound();

function playSound():void
{
    var channel:SoundChannel = mySound.play();
    channel.addEventListener(Event.SOUND_COMPLETE, onComplete);
}

function onComplete(event:Event):void
{
    SoundChannel(event.target).removeEventListener(event.type, onComplete);
    playSound();
}

This works perfectly.

like image 4
Enoinochi Avatar answered Nov 06 '22 12:11

Enoinochi


To expand on @scriptocalypse's gapless playback a bit:

The problem of not having proper gapless playback comes from mp3 including information about the file in either the head or the tail of the file (id3 tags etc), hence the small pause when you try to loop it. There are a few things you can do depending on your situation.

  1. Ignore it, just play as normal, with a small pause at the end of every file. You can also try and mask it with another sound (a beat drop yo), or fade out and fade in.
  2. If your sounds are embedded, and not streaming, then create a fla file, drag your mp3 in there, and set them to export (the same way you'd add a linkage name for a MovieClip etc). It seems that when you export sounds like this, Flash takes the delay into account, or strips it out when it creates the Sound object. Either way, you can just do a simple play() passing the loops that you want for a gapless playback (I've found using a loops parameter is better than waiting on the SOUND_COMPLETE event and playing it again).
  3. You can try some of the ogg libraries to use .ogg files instead of .mp3. A simple google search for "as3 ogg lib" will turn up what you need. Personally, I found them a bit awkward to use, and I couldn't afford the overhead added (as opposed to mp3 decoding, which is done in the player).
  4. If your mp3 files are streaming, then the only way to get gapless playback is to layer them. Determine the gap (depending on what you used to encode them, it'll be different - my files has a gap of about 330ms), and when you reach it, start playing the overlay. It's a proper pain if you're doing fading, but when it works, it works quite nicely. Worst case scenario, you end up with situation (1)
like image 3
divillysausages Avatar answered Nov 06 '22 13:11

divillysausages


I guess this what you looking for in case the voice/music file is in the library:

var mysound:my_sound = new my_sound();
mysound.play(0,2); // this will repeat the sound 2 times.
like image 2
user3623714 Avatar answered Nov 06 '22 13:11

user3623714