我有一个包含以下变量的SoundHandler类:
private static var musicChannel: SoundChannel;
private static var effectsChannel: SoundChannel;
private static var narrationChannel: SoundChannel;
private static var narrationMuted:Boolean;在initialize函数中:
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();
}
}任何帮助都是非常感谢的。谢谢!
发布于 2015-05-29 16:31:17
我猜你的初始化函数被调用了两次。每次调用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时添加跟踪命令: trace (“停止通道!”);narrationChannel.stop();var classReference: classReference= getDefinitionByName(narrationClassName) as Class;var s: classReference=getDefinitionByName();narrationClassName(“在通道中播放新声音!”)narrationChannel = s.play();
在初始化函数中执行此操作,以及播放旁白和开始或停止声音或通道的任何其他位置。我怀疑您会在控制台输出中看到以下内容:
Playing new sound in the channel!
Playing new sound in the channel!
Stopping the channel!这意味着您只需要在narrationChannel.stop()之前跟踪两次s.play()是如何被调用的
我可能完全错了,但对我来说,这似乎是最可能的解释。
https://stackoverflow.com/questions/30497144
复制相似问题