Copy link to clipboard
Copied
I am developing an eLearning module in Flash CS5 using Actionscript 3.
On most frames an audio file plays, and then the timeline moves to the next frame.
In the background of each frame are four buttons where a user can click to jump to a different section of the course.
The problem I am having is that sometimes when a user clicks to jump to a different section, the timeline jumps; but then after a short delay the timeframes starts jumping between the old frame and the new frame and playing both audio clips. I suspect this problem is only occurring if the audio file has not fully loaded when the user clicks the button; but this is my first time using audio in Flash so I could be completely wrong.
What I would like to do is change the code so that when a user clicks a button to jump somewhere else, the code says stop loading audio and stop playing any audio, and then jump to the new frame.
Here is the relevant sections of the code I have on layer 1, frame 1:
import flash.media.SoundMixer;
// Event listeners
mcWhenLarge.addEventListener(MouseEvent.CLICK, When);
mcWhatLarge.addEventListener(MouseEvent.CLICK, What);
// Event handlers
function When(event:MouseEvent):void
{
flash.media.SoundMixer.stopAll();
gotoAndPlay(7):
}
function What(event:MouseEvent):void
{
gotoAndPlay(2);
flash.media.SoundMixer.stopAll();
}
Layer 1 of most subsequent frames looks like this:
mcWhen1.addEventListener(MouseEvent.CLICK, When);
mcWhen1.buttonMode = true;
And then Layer 2 of most frames looks something like this:
import flash.net.URLRequest;
import flash.media.Sound;
import flash.events.Event;
var soundReq1:URLRequest = new URLRequest("Recording1.mp3");
var sound1:Sound = new Sound();
sound1.load(soundReq1);
sound1.addEventListener(Event.COMPLETE, onComplete1);
function onComplete1(event:Event):void
{
sound1.play();
setTimeout(gotoAndStop, 2000, 3);
}
Any help you can give me to stop this bug would be greatly appreciated.
M Hetherington
Copy link to clipboard
Copied
Would using gotoAndStop() commands instead of gotoAndPlay() commands help as far as not staying at the intended frames that you move to? (or are there frames that have to play when you arrive?)
Look into using the SoundChannel class for controlling the sound. What you could consider doing is to just have one Sound instance that you redefine as you move along the timeline, assigning it to a new sound at each frame.
Copy link to clipboard
Copied
Thanks for that Ned. I have changed the button statements to gotoAndStop() rather than gotoAndPlay.
So I'm hoping to do now is have one layer that has a single set of actions with a channel that will control all the sounds. I can't get it to work the way I want though. Here's what I've got:
// first I do a different variable for each sound file
var soundReq1:URLRequest = new URLRequest("Recording1.mp3");
var soundReq2:URLRequest = new URLRequest("Recording2.mp3");
// then I have the variables for the sound and the channel
var sound:Sound = new Sound ();
var soundControl:SoundChannel = new SoundChannel();
// then I tried to use entering a new frame to trigger a function to pick the right sound file
this.addEventListener(Event.ENTER_FRAME), whichSound);
function whichSound(event:Event):void
{
if(this.currentFrame == 2){
trace ("I found frame 2");
sound.load(soundReq1);
sound.play();
setTimeout(gotoAndStop, 2000, 3);
}
else if(this.currentFrame == 3){
trace ("I found frame 3");
sound.load(soundReq2);
sound.play();
setTimeout(gotoAndStop, 2000, 3);
}
}
It seems to be playing the audio; but I'm getting lots of "Error #2037: Functions called in incorrect sequence, or eariler call was unsuccessful" errors related to flash.media::Sound/_load().
Thanks in advance
M Hetherington
Find more inspiration, events, and resources on the new Adobe Community
Explore Now