Actionscript 3访问不同的动画片段时,同时播放多个声音

时间:2013-12-09 10:41:57

标签: actionscript-3 flash button movieclip

我正在尝试创建一个交互式电影,其结构使得时间轴中的每个关键帧都包含导航到使用影片剪辑内的按钮的动画片段,并在主时间轴内包含相应的代码。

现在的问题是,当您访问第3帧内的动画片段时,第2帧的声音也会同时播放。经过一些研究后,我发现这似乎是闪存本身的一个错误,大部分时间是使用SoundMixer.stopAll();来处理的。可悲的是,当只访问第3帧时,我不知道如何使用它来消除第2帧的声音。

我知道当访问第2帧时,只播放第2帧,这意味着闪存基本上会通过所有帧到达你应该去的帧。

这是我目前使用的有限代码:

第1帧:

import flash.events.MouseEvent;
import flash.display.SimpleButton;
import flash.media.SoundMixer;

stop();
var soundVar:int = 0;

var Choice1F:SimpleButton;
var Choice1R:SimpleButton;

this.Val1.Choice1F.addEventListener(MouseEvent.CLICK, function(me:MouseEvent):void{buttonHandler(me, 2)});
this.Val1.Choice1R.addEventListener(MouseEvent.CLICK, function(me:MouseEvent):void{buttonHandler(me, 3)});

function buttonHandler(e:MouseEvent, Value:int): void{
SoundMixer.stopAll();
soundVar = Value;
this.gotoAndPlay(Value);
}

第2帧:

import flash.media.SoundMixer;

stop();

if(soundVar == 3){
SoundMixer.stopAll();
}

第3帧只包含一个stop();声明。第2帧中的代码是一个徒劳的尝试,让它在第3帧的路上杀死声音。希望你们能想出更好的解决方案,如果有的话。

1 个答案:

答案 0 :(得分:1)

项目的正确结构假定您使用自定义类的特殊实例控制音乐和声音的播放。你使用的时间表只给了他什么时候和做什么的命令。 一个SoundChannel和几个Sound将会解决问题。

你可以使用这个

package src.utils{
    import flash.media.Sound;
    import flash.media.SoundChannel;
    import flash.net.URLRequest;
    public dynamic class BackgroundMusicPlayer extends Object{
        public var playlist;
        public var sndChannel;
        private var ID;
        public function BackgroundMusicPlayer(srcList:Array){
            playlist=[];
            for(var i=0;i<srcList.length;i++){
                var src= new URLRequest(srcList[i]);
                var newSound = new Sound(src);
                playlist.push(newSound);
            }
        }
        public function playMusic(id){
            if (sndChannel!=undefined) {
                sndChannel.stop();
            }
            sndChannel = playlist[id].play();
            ID=id;
            sndChannel.addEventListener("soundComplete",replayListener);
        }
        public function replayListener(e){
            sndChannel = playlist[ID].play();
        }
    }
}

将类导入到您的时间轴,创建实例传递给他的文件列表

var musicPlayer = new BackgroundMusicPlayer("music1.mp3","music2.mp3");

然后你想要开始一些声音,打电话

musicPlayer.playMusic(0);

如果你想使用导入项目声音,只需将它们分享到actionscript,给它们类名并略微修改给定的类构造函数

public function BackgroundMusicPlayer(srcList:Array){
    playlist=[];
    for(var i=0;i<srcList.length;i++){
         playlist.push(srcList[i]);
    }
}

所以现在你的实例创建应该是

var musicPlayer = new BackgroundMusicPlayer(new MySound1(),new MySound2());
相关问题