AS3 Pitfalls – SOUND_COMPLETE Event is Not Firing

PROBLEM:

The SOUND_COMPLETE event is not triggered when your sound finishes playing.

SOLUTION:

There are a few different reasons why this problem may occur, but here’s how it played out in my project:

When you’re listening for a SOUND_COMPLETE event, you work with two Classes: Sound and SoundChannel. The event listener goes on the SoundChannel instance:

var sound:Sound = new Sound("allYouNeedIsLove.mp3");
var soundChannel:SoundChannel = sound.play();
soundChannel.addEventListener(Event.SOUND_COMPLETE, soundIsFinished);

In my project, i rigged up a “Pause” button. When the player clicks “Pause”, i stop the sound and take note of the Sound’s position. When the player clicks “Pause” again, i unpause the game and tell the Sound to start playing where we left off.

But whenever you paused and unpaused the game, the SOUND_COMPLETE Event wouldn’t fire any more.

That’s because whenever you tell a Sound object to play, it returns a new SoundChannel instance. The old SoundChannel instance – the one with the event listener on it – is no longer relevant, so the event doesn’t fire. So to get this to work, when the player unpauses the game, you have to add the SOUND_COMPLETE event listener to the new SoundChannel instance that’s returned when you call Sound.play();

THANKS FOR BAILING ME OUT!

BIG thanks to user WearDark on the Kirupa forums for clearing this up, and for saving me from hours and hours of bug testing trying to figure out what was going on!

http://www.kirupa.com/forum/archive/index.php/t-263824.html

13 thoughts on “AS3 Pitfalls – SOUND_COMPLETE Event is Not Firing

  1. Rasmus Wriedt Larsen

    Yeah I’ve been having some trouble with the sound class as well. I think it’s so dammed that they made the class finished. Why would they even do that? Even I could have made so many improvements in a easier way if they would just let us extend it – but adobe says “NO!”.

    Reply
  2. exoboy

    Thanks! I had a sneaking suspician I might need to re-apply an event listener… but since I was not recreating the sound channel, I thought it listened to the channel and not an instance created by the SC… so thanks for saving me a few hours of hair-pulling. Cheers!

    Reply
  3. lowie

    Would have been tearing my hair out aswell if I still had any :). Many Thanks. Saved me much time.

    Reply
  4. Patrik

    Nice! Thank you alot.

    “That’s because whenever you tell a Sound object to play, it returns a new SoundChannel instance. The old SoundChannel instance – the one with the event listener on it – is no longer relevant, so the event doesn’t fire.”
    Solved all my problems! :-)

    Reply
  5. Damian Pope

    Wow! Just came across this through a Google search while I was having issues with SOUND_COMPLETE. It helped a bunch. Thanks Ryan!

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.