tailedmouse
tailedmouse

Reputation: 365

how to replay sound in actionscript 3

I am trying to replay this sound in my game...but for some reason it's working can somebody please explain me why?

   var morning:Sound =new alarmclock ();
                       var transforming:SoundTransform = new SoundTransform(0.5);
                       var morningChannel:SoundChannel = morning.play(0,0,transforming);
                       morningChannel.addEventListener(Event.SOUND_COMPLETE, replay);
                       function replay (event:Event) {
                    morningChannel = morning.play(0,0,transforming);    
                    trace ("ANYBODY IN THERE????");
                    }

Upvotes: 0

Views: 508

Answers (4)

Vipul
Vipul

Reputation: 431

try this :

import flash.media.Sound;

import flash.media.SoundChannel;

import flash.events.Event;

var demoSound:Sound;

var demoSoundChannel:SoundChannel;

playSound();

function playSound():void {

demoSound = new soundObj();// soundObj is your sound.
demoSoundChannel = new SoundChannel();
demoSoundChannel = demoSound.play();
demoSoundChannel.addEventListener(Event.SOUND_COMPLETE, onComplete);

}

function onComplete(e:Event):void {

playSound();

}

Just a simple example of loop playing, and it is working. you can add your parameters in play method, it will not impact in looping.

Upvotes: 1

abnvp
abnvp

Reputation: 1027

Do this:

function replay (event:Event) {
  morningChannel = morning.play(0,0,transforming);    
  trace ("ANYBODY IN THERE????");
  morningChannel.addEventListener( Event.SOUND_COMPLETE, replay );
}

[Note we've added the event listener to the sound channel again. This is because the line "morningChannel = morning.play(0,0,transforming);" causes all event listeners on Sound Channel to be lost.]

I have borrowed the explanation from http://gamedev.michaeljameswilliams.com/2009/03/03/avoider-game-tutorial-9/

Upvotes: 1

Sunil D.
Sunil D.

Reputation: 18193

Try listening for Event.COMPLETE that gets dispatched by the Sound object. I've successfully used that in the past to replay a sound.

I didn't know that the SoundChannel dispatches it's own Event.SOUND_COMPLETE event. Both your code (and the similar code from @Lee Burrows) seems like it should work.

In any case, listening for Event.COMPLETE on the Sound object might be worth trying:

var morningChannel:SoundChannel = morning.play(pausing,1,transforming);
morning.addEventListener(Event.COMPLETE, replay);

function replay (event:Event)
{
    morningChannel = morning.play(pausing,1,transforming);               
}

If this doesn't work either, it could be possible that something is getting garbage collected.

Also, you are passing in 1 to the play() method so it loops once. Is that working? Perhaps that is somehow confusing things. You might try setting that to 0, since you are replaying the sound anyway.

Finally, if it's still not working you should set a breakpoint in the replay() method (or add a trace() statement in it) so you can determine if the event is getting dispatched or not.

Upvotes: 0

user1901867
user1901867

Reputation:

try:

function replay (event:Event)
{
    morningChannel = morning.play(pausing,1,transforming);               
}

Upvotes: 0

Related Questions