Here’s a Handy little tip I just figured out today. So I have a button that plays sound and once if finishes playing it should fire the Event.SOUND_COMPLETE. In this events handler, it resets the position back to the beginning of the clip. Now the problem is once I try to play it again, it doesn’t seem to fire the Event.SOUND_COMPLETE event. Have you ever had the issue of your Event.SOUND_COMPLETE not firing properly?? Read on and find out why……
After some debugging, I noticed that every time you pause a sound and play it again, a new SoundChannel is returned. Any event listeners I had applied to the SoundChannel before are disregarded once I pause and play. Instead of :
function playSound():void
{
mySound : Sound = new Sound();
mySound.addEventListener(Event.COMPLETE, soundLoaded);
mySound.load(MY SONG REQUEST);
}
function soundLoaded(e : Event):void
{
myChannel = new SoundChannel();
myChannel.addEventListener(Event.SOUND_COMPLETE, soundComplete);
myChannel = mySound.play();
}
function soundComplete(e : Event):void
{
position = 0;
playChannel()
pauseChannel();
myChannel.addEventListener(Event.SOUND_COMPLETE, soundComplete);
}
function pauseChannel():void
{
myChannel.stop();
position = myChannel.position;
}
function playChannel():void
{
myChannel = mySound.play(position);
}
But It should be :
function playSound():void
{
mySound : Sound = new Sound();
mySound.addEventListener(Event.COMPLETE, soundLoaded);
mySound.load(MY SONG REQUEST);
}
function soundLoaded(e : Event):void
{
myChannel = new SoundChannel();
myChannel.addEventListener(Event.SOUND_COMPLETE, soundComplete);
myChannel = mySound.play();
}
function soundComplete(e : Event):void
{
position = 0;
playChannel();
pauseChannel();
}
function pauseChannel():void
{
myChannel.stop();
position = myChannel.position;
}
function playChannel():void
{
myChannel = mySound.play(position);
myChannel.addEventListener(Event.SOUND_COMPLETE, soundComplete);
}
You need to register the event listener again once you call play. I hope this saves 30 minutes of your day once you start screaming…WHERE’S MY SOUND!!!