Jump to content
GreenSock

Benjamin Waller

Sound Issues - MP3Loader & Child swfs

Recommended Posts

hi again,

I'm experiencing sound issues with MP3Loader when loading a series of child swfs sequentially.

In this example, I'm loading 2 child swfs from Parent swf at once and playing then sequentially. Each child swf has it own MP3Loader’s with both autoPlay methods set to false. So far, the first Child swf plays first and when it completes the next Child swf starts as it is supposed to do.

However, when the first sound plays from the first Child swf so does the first sound from the second Child swf at the same time. I don’t know how to stop this sound from playing until it should.

 

The other related issue is the when the first Child swf is load for the second time, because its set up to play in a loop, the first sound isn’t being reset so that it doesn’t play again as it should.

 

Here the code from one of the Child swfs.

import com.greensock.*;
import com.greensock.easing.*;
import com.greensock.loading.*;
import com.greensock.events.LoaderEvent;
import com.greensock.plugins.TweenPlugin;
import com.greensock.plugins.AutoAlphaPlugin;
import com.greensock.plugins.VisiblePlugin;

TweenPlugin.activate([AutoAlphaPlugin, VisiblePlugin]);

import flash.events.MouseEvent;
import flash.events.Event;
import flash.display.Loader;
import com.greensock.events.TweenEvent;
import flash.display.MovieClip;
import flash.events.ActivityEvent;


this.Word1.visible = false;
this.Word2.visible = false;
this.cross1.visible = false;
this.cross2.visible = false;
this.tick1.visible = false;
this.tick2.visible = false;

this.cross1.alpha = 0;
this.cross2.alpha = 0;
this.tick1.alpha = 0;
this.tick2.alpha = 0;


////Greensock mp3Loader ////
var sound1:MP3Loader = new MP3Loader("sound/tick.mp3", {autoPlay:false});
var sound2:MP3Loader = new MP3Loader("sound/spring.mp3", {autoPlay:false});
var sound3:MP3Loader = new MP3Loader("sound/chào_Em.mp3", {autoPlay:false});
var sound4:MP3Loader = new MP3Loader("sound/chào_Anh.mp3", {autoPlay:false});
//var currentLoader:SWFLoader;

//{onComplete:init}
var queue:LoaderMax = new LoaderMax({paused:true});

queue.append(sound1);
queue.append(sound2);
queue.append(sound3);
queue.append(sound4);
queue.load();


// ObjectsIn array holds all assets (except for the Word1 & Word2 2nd mcs) that tween on stage independently to start the activity
// ObkectsOut array holds all assets which tweens them to an Alpha of O when the activity is completed
var ObjectsIn:Array = [one, two, three, four, Target1, Target2];
var ObjectsOut:Array = [one, two, three, four, Target1, Target2, Word1, Word2];
var mainIn:TimelineMax = new TimelineMax({delay:2}); // 
var mainOut:TimelineMax = new TimelineMax({paused:true});

mainIn.appendMultiple(TweenMax.allFrom(ObjectsIn, 3, {autoAlpha:0}));// all Obj are tweened on stage after 2 seconds 
//this object triggers the first sound to be played
mainIn.append(TweenLite.from(Word1, 0.2, {autoAlpha:0, onStart:play1stSound}), .02); 

mainOut.appendMultiple(TweenMax.allTo(ObjectsOut, 3, {autoAlpha:0}), 3); // all Obj are tweened off stage after 2 seconds

var Tick1:TimelineLite = new TimelineLite({paused:true});
var Tick2:TimelineLite = new TimelineLite({paused:true});
var Cross1:TimelineLite = new TimelineLite({paused:true});
var Cross2:TimelineLite = new TimelineLite({paused:true}); 



Tick1.append( new TweenLite(tick1, 1.2, {autoAlpha:1, onComplete:ReverseTick1}));
Tick2.append( new TweenLite(tick2, 1.2, {autoAlpha:1, onComplete:ReverseTick2}));
Cross1.append( new TweenLite(cross1, 1.2, {autoAlpha:1, onComplete:ReverseCross1}));
Cross2.append( new TweenLite(cross2, 1.2, {autoAlpha:1, onComplete:ReverseCross2}));

//Event Listeners for the 2 containers that are clickable with a glow filter ///
one.buttonMode = true; 
two.buttonMode = true; 
Word1.soundIcon1.buttonMode = true;
Word2.soundIcon2.buttonMode = true;
three.soundIcon3.buttonMode = true;
four.soundIcon4.buttonMode = true;
//Word1.invisBut.buttonMode = true;
one.addEventListener(MouseEvent.ROLL_OVER, objOver);
two.addEventListener(MouseEvent.ROLL_OVER, objOver);
one.addEventListener(MouseEvent.ROLL_OUT, objOut);
two.addEventListener(MouseEvent.ROLL_OUT, objOut);

//Event Listeners for sound icons that are clickable ///
Word1.soundIcon1.addEventListener(MouseEvent.CLICK, play1stWordSound);
Word2.soundIcon2.addEventListener(MouseEvent.CLICK, play2ndWordSound);
three.soundIcon3.addEventListener(MouseEvent.CLICK, play1stWordSound);
four.soundIcon4.addEventListener(MouseEvent.CLICK, play2ndWordSound);


/// Glow function for current target to show user which buttons can be selected/////
function objOver(e:MouseEvent):void{

   var currentMC:MovieClip = MovieClip(e.target);
   //TweenMax.to(currentMC, .3, {colorMatrixFilter:{saturation:1, brightness:1}});
   TweenMax.to(currentMC, 0, {glowFilter:{color:0xffffff, alpha:1, blurX:40, blurY:40}});

   }

function objOut(e:MouseEvent):void{

   var currentMC:MovieClip = MovieClip(e.target);
   //TweenMax.to(currentMC, 1, {colorMatrixFilter:{saturation:.1, brightness:.5}});
   TweenMax.to(currentMC, 0, {glowFilter:{color:0xffffff, alpha:0, blurX:40, blurY:40}});
}  

///Button Sequencer Class /////// by Jody Hall

var buttonArray:Array = [one, two];

var enforcer:ButtonSequencer = new ButtonSequencer(buttonArray);

enforcer.addEventListener(ButtonSequencer.CORRECT, correct_onClick);
enforcer.addEventListener(ButtonSequencer.INCORRECT, incorrect_onClick);



/// This function shows ticks, plays sound for correct selections & tweens the correct word into place on the matching pictures////
function correct_onClick(event:Event):void {
trace("correct");
if(enforcer.lastClicked == one) {
	trace("one clicked");
	//tween the correct word into position
	TweenLite.to(Word1, 1, {x:Target1.x, y:Target1.y, ease:Quad.easeInOut, onComplete:DelayCall});
	Tick1.play();// show the tick graphic
	enforcer.enabled = false;
} else if(enforcer.lastClicked == two) {
	trace("two clicked");
	 // tween the correct word into position
	TweenLite.to(Word2, 1, {x:Target2.x, y:Target2.y, ease:Quad.easeInOut});
	Tick2.play(); // tween the tick graphic
	TweenLite.to(Word1, 2, {autoAlpha:1}); // retween Word1 visible
	this.Word1.soundIcon1.visible = false;
	mainOut.play(); //tween all ojbs on stage (except anh_word) and waits 4 sec and Alpha's out.
	enforcer.enabled = false;
	one.buttonMode = false;
	three.soundIcon3.buttonMode = false;
	four.soundIcon4.buttonMode = false;
	Word1.soundIcon1.removeEventListener(MouseEvent.CLICK, play1stWordSound);
	Word2.soundIcon2.removeEventListener(MouseEvent.CLICK, play2ndWordSound);
	three.soundIcon3.removeEventListener(MouseEvent.CLICK, play1stWordSound);
	four.soundIcon4.removeEventListener(MouseEvent.CLICK, play2ndWordSound);
}

 sound1.playSound();///play sound for correct
 sound1.gotoSoundTime(0,true);
}

//// This function shows incorrect clicks for both wrong selections. 
function incorrect_onClick(event:Event):void {
trace("sorry, try again");
if(enforcer.lastClicked == one) {
	trace("one clicked");
	Cross1.play();
	sound2.playSound();///play sound for incorrect
	sound2.addEventListener(Event.SOUND_COMPLETE, done1);
} else if(enforcer.lastClicked == two) {
	trace("two clicked");
	Cross2.play();
	sound2.playSound();
	sound2.addEventListener(Event.SOUND_COMPLETE, done2)
}
sound2.gotoSoundTime(0,true);
}

///Sound handling functions////
function play1stSound():void{
trace("first sound played from child swf 1"); 
//sound3.gotoSoundTime(0,true);
sound3.playSound();
}

function done1(e:Event):void{
trace("wait while loading the second sound");
TweenLite.delayedCall(0.2, playNextSound1)
//kill any delayed calles to playNextSound
TweenLite.killTweensOf(playNextSound2);
//pause sound2 incase it is playing
 sound3.pauseSound();
}	

function playNextSound1(){
trace("let's play the second sound again");
sound4.gotoSoundTime(0,true);
}	

function done2(e:Event):void{
trace("wait while loading the first sound");
TweenLite.delayedCall(0.2, playNextSound2)
TweenLite.killTweensOf(playNextSound1);
sound4.pauseSound();
}	

function playNextSound2(){
trace("let's play the first sound again");
sound3.gotoSoundTime(0,true);
}	

function DelayCall(){
Word1.soundIcon1.buttonMode = false;
Word1.soundIcon1.removeEventListener(MouseEvent.CLICK, play1stWordSound);
TweenLite.delayedCall(2, SetUpNextWord)


}
function SetUpNextWord(){
TweenLite.to(Word1, 2, {autoAlpha:0});// fade out 1st word
TweenLite.to(Word2, 2, {autoAlpha:1});// fade in 2nd word
enforcer.enabled = true;
one.buttonMode = true; 
trace("second sound played from child swf 1"); 
sound4.gotoSoundTime(0,true);
}

function ReverseTick1(){
	Tick1.reverse();// fade out the tick graphic 1
	one.buttonMode = false; 
	}
function ReverseTick2(){
	two.buttonMode = false; 
	Tick2.reverse(); // fade out the tick graphic 2
	}
function ReverseCross1(){
	Cross1.reverse();// fade out the cross graphic 1
	queue.load();
	}
function ReverseCross2(){
	Cross2.reverse(); //fade out the cross graphic 2
	queue.load();
	}

//Sounds functions for the sound icons 
function play1stWordSound(event:Event){
	//enforcer.enabled = true;
	trace("sound played from 1st Word icon"); 
	sound3.gotoSoundTime(0,true);
	}

function play2ndWordSound(event:Event){
	//enforcer.enabled = true;
	trace("sound played from 2nd Word icon"); 
	sound4.gotoSoundTime(0,true);
	}

function swfComplete(){
trace("Activity complete");
var parentMovie:MovieClip = this.parent.parent as MovieClip;
parentMovie.timelineCompleteMessage();
//currentLoader.rawContent.startTimelineMax(); 
}
function notifyParent(){
var parentMovie:MovieClip = this.parent.parent as MovieClip;
parentMovie.timelineStartMessage();
}

 

See files attached.

Link to comment
Share on other sites

I've looked over the 200+ lines of code above and your attached files.

 

The only thing I can guess is that the mainIn TimelineMax isn't paused so it plays as soon as the swf that it is in has loaded.

 

Other than that it is really difficult to visualize how all that code is supposed to work. The only thing I can suggest is that if you need more assistance you are going to have to provide much simpler files that focus on illustrating a singular issue. Although that is not an easy task, more often than you will solve the problem while trying to recreate it without a lot of additional logic and code baggage.

Link to comment
Share on other sites

Hi Carl,

 

Thanks for having a look.

I was sort of hoping you might have found a glaring problem & a quick fix so I wouldn't have to strip it back but that was just wishful thinking!

 

I tried pausing that mainIn TimelineMax instance however it just meant it didn't play at all. The issue does seem to be related to the loaded sound files only because the mainIn TimelineMax instance in the 2nd Child swf loads in and plays just fine after the 1st Child swf's TimeLineMax animation is completed. So again it seems to be the way the sound is loaded in or set up that is triggering the sound to play prematurely from the 2nd Child swf.

But I appreciate your time & efforts.

 

I'll see what I can do.

 

Thanks again.

Ben.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×