繁体   English   中英

Actionscript 3访问不同的影片剪辑时同时播放多种声音

[英]Actionscript 3 Multiple sounds playing at the same time when accessing different movieclips

我现在正在尝试创建一个交互式电影,其结构应使时间轴中的每个关键帧都包含一个影片剪辑,该影片剪辑可以使用影片剪辑中的按钮导航至主时间轴中的相应代码。

现在的问题是,当您访问第3帧内的动画片段时,第2帧的声音也会同时播放。 经过研究后,我发现这似乎是Flash本身的错误,大部分时间是使用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();
}

第三帧只包含一个stop(); 声明。 框架2中的代码对我来说是徒劳的尝试,目的是使它在进入框架3的过程中杀死声音。希望,即使存在,您也可以想到一个更好的解决方案。

项目的正确结构假定您使用特殊的自定义类实例来控制音乐和声音的播放。 而且您使用的时间轴只给了他命令何时以及如何执行。 一个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);

如果要使用导入的声音来投射声音,只需将其共享给动作脚本,为其指定类名,并稍稍修改给定的类构造函数

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM