0
votes

I am at the moment trying to create an interactive movie, structured so that each keyframe in the timeline contains a movieclip navigated to using buttons inside the movieclips, with the appropriate code inside the main timeline.

The problem right now is that when you access the movieclip inside frame 3, the sound from frame 2 also plays simultaneously. After doing some research i found that this appears to be a bug with flash itself, and most of the time is dealt with using SoundMixer.stopAll();. Sadly, i have no idea how to use it to kill the sound from frame 2 when only frame 3 is accessed.

I know that when accessing frame 2 instead, only frame 2 is played, which should mean that flash basically goes through all frames on the way to the frame you are supposed to go to.

This is the limited code i am using at the moment:

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

Frame 2:

import flash.media.SoundMixer;

stop();

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

Frame 3 simply contains a stop(); statement. The code in frame 2 was a futile attempt from me to make it kill the sound on its way to frame 3. Hopefully, you guys can think of a better solution, if one even exists.

1
PS: stop() is a function - Cilan

1 Answers

1
votes

The correct structure of the project assumes that you control the playback of music and sounds with a special instance of custom class. And timeline you use only gave him command when and what to do. One SoundChannel and couple of Sound's will do the trick.

You could use this one

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

import class to you timeline, create instance passing him files list

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

And then you want start some sound, call

musicPlayer.playMusic(0);

If you want use imported to project sounds, just share them to actionscript, give them class names and slightly modify given class constructor

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

So your instance creation now should be

var musicPlayer = new BackgroundMusicPlayer(new MySound1(),new MySound2());