我有一个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();
}
}
感谢任何帮助。谢谢!
答案 0 :(得分: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
尝试此操作 - 每次在代码中的任何位置启动或停止s
或narrationChannel
时添加跟踪命令:
跟踪(“停止频道!”);
narrationChannel.stop();
var classReference:Class = getDefinitionByName(narrationClassName)as Class;
var s:Sound = new classReference();
跟踪(“在频道播放新声音!”)
narrationChannel = s.play();
在初始化功能中执行此操作以及播放旁白以及开始或停止声音或频道的任何其他位置。我怀疑你会在你的控制台输出中看到这个:
Playing new sound in the channel!
Playing new sound in the channel!
Stopping the channel!
这意味着您只需要跟踪s.play()
narrationChannel.stop()
的方式
我可能完全错误,但这似乎是对我最有可能的解释。