Noah R
Noah R

Reputation: 5477

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

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

Upvotes: 9

Views: 63036

Answers (8)

Rudianto Sihombing
Rudianto Sihombing

Reputation: 1

this work for me :

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

var soundUrl:String ="music.mp3";
var soundChannel:SoundChannel = new SoundChannel();
var sound:Sound = new Sound();

sound.load(new URLRequest(soundUrl));
soundChannel = sound.play();
soundChannel.addEventListener(Event.SOUND_COMPLETE,onComplete);

function onComplete(e:Event):void{
    sound = new Sound();
    sound.load(new URLRequest(soundUrl));
    soundChannel = sound.play();
    soundChannel.addEventListener(Event.SOUND_COMPLETE,onComplete);
}

Upvotes: 0

madgineer
madgineer

Reputation: 11

This appears to have worked for me:

var nowTime:Number = (new Date()).time;
var timeElapsed:Number = nowTime - _lastTime;

_lastTime = nowTime;
_musicTimeElapsed+=timeElapsed;

if(_musicTimeElapsed >= _musicA.length - GAP_LENGTH)
{
   _musicTimeElapsed = 0;
   _musicA.play(0);
}

Upvotes: 1

user3623714
user3623714

Reputation: 21

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.

Upvotes: 2

eishiakonno
eishiakonno

Reputation: 73

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.

Upvotes: 4

divillysausages
divillysausages

Reputation: 8053

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)

Upvotes: 3

Olin Kirkland
Olin Kirkland

Reputation: 545

The other answers are great, however if you do not want to use code (for whatever reason), you can put the sound in a movieclip, set the sound property to "Stream", and then add as many frames as you like to the movie clip to ensure it plays fully.

This, of course, is a less preferred way, but for animators I'm sure it may be preferable in some situations (for example when synced with mouth animations that the animator wants looped).

Upvotes: 0

scriptocalypse
scriptocalypse

Reputation: 4962

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.

Upvotes: 27

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();
}

Upvotes: 10

Related Questions