0

我有一个带有以下变量的 SoundHandler 类:

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

在初始化函数中:

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

效果和音乐频道工作正常,但在调用 stop() 时旁白频道不会停止。这是功能:

    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() 确实停止了旁白的声音,但我不能使用它,因为它也会停止音乐和效果的声音。

我怀疑 stop() 由于我创建 Sound 对象的方式不起作用,但我不确定。从外部加载并不能解决问题:

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

将旁白听起来作为静态变量也不起作用:

private static var subNarr1:Sound;

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

任何帮助表示赞赏。谢谢!

4

1 回答 1

1

我的猜测是你的初始化函数被调用了两次。每次调用时,narrationChannel = s.play();您都会丢失对前一个实例的引用。

例如,如果您这样做:

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

试试这个 - 每次启动或停止snarrationChannel在代码中的任何位置添加跟踪命令:trace("Stopping the channel!"); narrationChannel.stop(); var classReference: Class = getDefinitionByName(narrationClassName) as Class; var s: Sound = new classReference(); trace("正在频道中播放新声音!") narrationChannel = s.play();

在您的初始化功能以及播放旁白以及您启动或停止声音或频道的任何其他地方执行此操作。我怀疑你会在控制台输出中看到这个:

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

这意味着您只需要跟踪s.play()之前如何被调用两次narrationChannel.stop()

可能完全错了,但这对我来说似乎是最有可能的解释。

于 2015-05-29T08:31:17.453 回答