0

I have a SoundHandler class with the following variables:

private static var musicChannel: SoundChannel;
private static var effectsChannel: SoundChannel;
private static var narrationChannel: SoundChannel;
private static var narrationMuted:Boolean;

In the initialize function:

var classReference: Class = getDefinitionByName(narrationClassName) as Class; 
var s: Sound = new classReference(); 
narrationChannel = s.play();

The effects and music channels work fine, but the narration channel doesn't stop when stop() is called. Here is the function:

    public static function playNarration(narrationClassName: String): void {
        if (!narrationMuted) {
            narrationChannel.stop(); //NOT WORKING--THE SOUND KEEPS PLAYING!
            var classReference: Class = getDefinitionByName(narrationClassName) as Class;
            var s: Sound = new classReference();
            narrationChannel = s.play();
        }
    }

SoundMixer.stopAll() DOES stop the narration sound, but I can't use it because it also stops the music and effects sounds.

I suspect that stop() isn't working because of how I'm creating the Sound object, but I'm not sure. Loading externally doesn't fix the problem:

     public static function playNarration(narrationClassName: String): void {
            if (!narrationMuted) {
                narrationChannel.stop();
                var s: Sound = new Sound();
                s.addEventListener(Event.COMPLETE, onNarrationSoundLoaded);
                var req: URLRequest = new URLRequest("sounds/Narration/sub_narr_1.mp3");
                s.load(req);
            }
        }
        private static function onNarrationSoundLoaded(e: Event): void {
            var localSound: Sound = e.target as Sound;
            narrationChannel = localSound.play();
        }

Keeping the narration sounds as static variables doesn't work either:

private static var subNarr1:Sound;

public static function playNarration(narrationClassName: String): void {
            if (!narrationMuted) {      
                narrationChannel.stop();
                narrationChannel = subNarr1.play();
            }
        }

Any help is appreciated. Thanks!

user45623
  • 621
  • 4
  • 18
  • Could `narrationChannel.stop();` not working because `narrationChannel` is `null`? Based on your posted codes, if `playNarration(...)` is executed before other functions, `narrationChannel` might be `null`? – Marson Mao May 28 '15 at 05:46
  • Are you sure `!narrationMuted` condition is met? Does the rest of the code work that is beneath it? Try using ` trace("Something") ` and check if the condition meets. – Mana May 28 '15 at 09:58
  • It seems like you are leaving something out. Where are you first instancing the narrationChannel? Your play method starts out by stopping the narration channel, but how could you stop it if you haven't started it yet? Do you first start it somewhere else? – user45623 May 28 '15 at 23:26
  • @Mana Yes, the !narrationMuted condition is met. – Dora Erikkson May 29 '15 at 06:01
  • @user45623 You are correct, I left out the code where I start the narration channel. It's in an initialize function that looks like this: var classReference: Class = getDefinitionByName(narrationClassName) as Class; var s: Sound = new classReference(); narrationChannel = s.play(); – Dora Erikkson May 29 '15 at 06:02

1 Answers1

1

My guess is that your initialization function is getting called twice. Each time you call narrationChannel = s.play(); you lose the reference to the previous instance.

For example, if you do this:

narrationChannel = s.play(); //narrationChannel points to what we'll call "instance 1"
narrationChannel = s.play(); //narrationChannel points to what we'll call "instance 2"
narrationChannel.stop(); //you just stopped 'instance 2' but 'instance 1' is still playing

Try this out - Add trace commands every time you start or stop s or narrationChannel anywhere in your code: trace("Stopping the channel!"); narrationChannel.stop(); var classReference: Class = getDefinitionByName(narrationClassName) as Class; var s: Sound = new classReference(); trace("Playing new sound in the channel!") narrationChannel = s.play();

Do this in your initialize function as well as play narration and anywhere else you start or stop the sound or channel. I suspect that you will see this in your console output:

Playing new sound in the channel!
Playing new sound in the channel!
Stopping the channel!

which means you just need to track down how s.play() is getting called twice before narrationChannel.stop()

I could be completely wrong, but this seems like the most likely explanation to me.

user45623
  • 621
  • 4
  • 18