FORUMS: list search recent posts

removeChild doesn't kill the audio in external swf (as3)

COW Forums : Adobe Flash

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Steve Parker
removeChild doesn't kill the audio in external swf (as3)
on Apr 19, 2009 at 4:15:15 pm

I am have several external swfs that get loaded into a container swf using addChild. Each time the user uses the navigation to select a new page I use addChild to swap out external swfs. This all works fine except a few of my external swfs have audio, and for some reason the audio keeps playing even when that swf has been removed and replaced with a new one.

I've looked all around for solutions and found several people with a similar issue, but no solution.


Return to posts index

cowcowcowcowcow
demetri tashie
Re: removeChild doesn't kill the audio in external swf (as3)
on Apr 19, 2009 at 4:59:17 pm

we've actually talked about this issue several times, in various iterations.

place this snippett of code for when you trigger the loading of your new/next swf into the container:

SoundMixer.stopAll();

i would place it on the first line of the function used to load the next swf

removeChild() removes that instance ( in this case an external SWF) from the Display List, but it does not clear the Sound Channel of the parent ( loader) SWF. this code will take care of that.

let us know how it works out for you.



Return to posts index

Steve Parker
Re: removeChild doesn't kill the audio in external swf (as3)
on Apr 19, 2009 at 4:58:40 pm

Thanks for the suggestion. I actually had already tried SoundMixer.stopAll(); and it didn't seem to work. I thought maybe that bit of code was only affecting the audio (if there was any) in the container swf and not the external swfs.

Is there something I need to do so that that bit of code is directed towards the external swf?


Return to posts index


demetri tashie
Re: removeChild doesn't kill the audio in external swf (as3)
on Apr 19, 2009 at 5:52:29 pm

as long as you are placing that code correctly, it will work. it does just that: tells the Sound Mixer to stop all sounds,effrectively clearing it. just make sure you are telling the main timeline / the loader, and not the loaded sfws to do this.

tell me how you have your file set up ( ie a main time line, that has a loader movie clip, that loads external swfs, the main timeline has button(s) that tell the loader what to load, etc )

i will be out for a few hours, but willlook back later ....


Return to posts index

Steve Parker
Re: removeChild doesn't kill the audio in external swf (as3)
on Apr 19, 2009 at 6:37:32 pm

Unfortunately I'm not in front of the project right now, but will be Monday. Before I waste any of your time I will double check that I was using the sound mixer code in the right spot as soon as I get a chance.

Trying to recreate this from memory so it might be a little off, but this is an abbreviated version of how my file is set up (didn't put my event listeners or anything but you get the idea...)


//create loader
var swfLoader:Loader = new Loader();
//url request for external swfs (i have one for each of my SWFs)
var section1SWFRequest:URLRequest = new URLRequest("section1.swf");
var section2SWFRequest:URLRequest = new URLRequest("section2.swf");

//when a section in the nav is clicked do this
function section1Clicked(event:MouseEvent):void
{
//i think this is where i tried tell all audio to stop
SoundMixer.stopAll();
//get rid of the old external swf
removeChild(swfLoader);
//load the new external swf
swfLoader.load(section1SWFRequest);
addChildAt(swfLoader, 0);
}


Hopefully the first time i tried that i just had the stopAll code in the wrong spot...we'll see what happens monday!


Return to posts index

demetri tashie
Re: removeChild doesn't kill the audio in external swf (as3)
on Apr 20, 2009 at 2:31:53 am

so i got back, and made a sample test, and, as i thought, it works just fine. i created 2 external SWFs, "video1" and "video2" each of those has a FLVPlayback Component that auto plays a video(with sound !)

without the 'snippet of code'(SounMixer.stopAll(); ) the sound from one plays on top of the other.

once the code is added, the sound is killed each time before the new swf is loaded - just what we want.

here is my code if you want to check against it, modify it etc. let me kno whow it works for you,or what you come up with.

BTW - your code and strategy looks just fine to me. the possible conflict i see is that you are adding a Child at a specific asset depth/"z-order" ( in your case at "0" ), but you are just using "removeChild" rather than "removeChildAt" i am not sure if that makes a difference or causes a problem.

but -unless you need that specific z-order for specific stacking reasons, ( or at least for now, to trouble-shoot this,) drop the z-order and just use addChild and removeChild, as per my code. also, i assume you have some swf initially loaded? otherwise there could be a conflict when a 'removeChild' is called if there is no child established yet.

i commented out the initial load of a swf, and the removeChild code, which would be used if there was an initial load, and not needed at all if there is no inital loaded swf.


var firstRequest:URLRequest=new URLRequest("video1.swf");
var secondRequest:URLRequest=new URLRequest("video2.swf");
var swfLoader1:Loader=new Loader();
//swfLoader1.load(firstRequest);
//addChild(swfLoader1);

btn1.addEventListener(MouseEvent.CLICK, btnClick1);
btn2.addEventListener(MouseEvent.CLICK, btnClick2);

function btnClick1(event:Event):void {
SoundMixer.stopAll();
//removeChild(swfLoader1);
swfLoader1.load(firstRequest);
addChild(swfLoader1);
}

function btnClick2(event:Event):void {
SoundMixer.stopAll();
//removeChild(swfLoader1);
swfLoader1.load(secondRequest);
addChild(swfLoader1);
}

let me know if this makes sense and helps


Return to posts index


Steve Parker
Re: removeChild doesn't kill the audio in external swf (as3)
on Apr 20, 2009 at 3:50:26 am

Yes all this makes sense, thanks for all the troubleshooting suggestions as well. I will give it a shot tomorrow(don't have access to flash right now). Do you think it could make a difference that I am using mp3 clips and not FLVs? I am using actionscript to load the audio clips to the stage (they range from 10 - 40 seconds, though one of them I'd like to have looping).

I do need to specify the depth, otherwise the loaded swf covers up the navigation in my container swf. I will try removeChildAt instead as well as just addChild and removeChild for testing purposes if need be. And yes I do initially load an external swf before anything is clicked.

Can't wait to hopefully get this solved! I'll let you know what happens, thanks for your time and help!


Return to posts index

demetri tashie
Re: removeChild doesn't kill the audio in external swf (as3)
on Apr 20, 2009 at 11:57:07 am

no, it will not make any difference that you are using mp3s loaded via actionscript as the sound.
it will work the same exact way.

also, i am sure you are aware, but i will just point out anyway, just in case, that you can have the Loader ( in my case named 'swfLoader1' ) positioned anywhere - so that it would show up below the nav bar, in your case, as opposed to its default position at x=0 and y=0. see the 2 commented out lines of code below that would take care of that ( IF that is the only consideration of why you were using "addChildAt" rather than just 'addChild" . just some food for thought ...)



var firstRequest:URLRequest=new URLRequest("video1.swf");
var secondRequest:URLRequest=new URLRequest("video2.swf");
var swfLoader1:Loader=new Loader();
swfLoader1.load(firstRequest);
addChild(swfLoader1);
//swfLoader1.x=50;
//swfLoader.y=150;

btn1.addEventListener(MouseEvent.CLICK, btnClick1);
btn2.addEventListener(MouseEvent.CLICK, btnClick2);

function btnClick1(event:Event):void {
SoundMixer.stopAll();
removeChild(swfLoader1);
swfLoader1.load(firstRequest);
addChild(swfLoader1);
}

function btnClick2(event:Event):void {
SoundMixer.stopAll();
removeChild(swfLoader1);
swfLoader1.load(secondRequest);
addChild(swfLoader1);
}



Return to posts index

Steve Parker
Re: removeChild doesn't kill the audio in external swf (as3)
on Apr 20, 2009 at 2:35:40 pm

Ok, I've been testing and testing and haven't had any luck. I even decided to dumb everything down and create a bare bones version. No luck with that, so I decided to use your code in case I was doing something funky in my code. Still no luck though.

I'm posting my test files, hopefully you could take a quick look and see what I am doing wrong? Basically the 1st swf that is loaded has a delayed audio clip, clicking the > arrow loads the 2nd swf which has no audio. clicking the < arrow loads the 1st swf again. If you click the > arrow before the audio has played you will still hear it play after the 2nd swf has been loaded.

Hopefully I am just missing something really silly...

source files: 30_testexternalswf.zip


Return to posts index


Steve Parker
Re: removeChild doesn't kill the audio in external swf (as3)
on Apr 20, 2009 at 3:27:58 pm

ah ha! apparently the issue is with the fact that the audio is delayed! if the audio is on the first frame, then there is no problem, it gets muted! very frustrating, but i'm glad i've got that figured out. now i just have to find a way around that.

this may not be the cleanest fix, but i suppose i could just add the delay into my audio clip since we are only talking a couple seconds of delay. i'm not crazy about that solution, but i might have to go with it.

it still bothers me that removeChild doesn't take care of this issue in the first place. seems like a bit of an as3 bug to me, no?

thanks again for your time!


Return to posts index

demetri tashie
Re: removeChild doesn't kill the audio in external swf (as3)
on Apr 20, 2009 at 3:51:10 pm

i am glad you are figuring this out. i will look at your files and do another test - this time with delayed audio.

so now, withopout looking at your files yet, but from your desription, your externally loaded swf(s) are multi-framed? with a stop action on the first frame ? let me now look at the files. in the meantime:

the way i understand the 'addChild' thing, especially with an external swf that has audio is this:

the 'addChild' method adds the file to the Display List ( in this case a Loader) of the 'main timeline' .
what is happening in the laoded swf is independent. in our cases, audio, or video with audio, is added to the externally loaded swf. any video is a display object OF THE LOADED SWF, as well as anything else contained within that swf. the audio in that loaded swf, from whatever source you set, gets 'loaded' into the maintimeline's Sound Channel(s). so by removing the child, ie, the external clip containing the sound, the DISPLAY object is removed from the Loader, but the audio has not been told to do anything different than keep on playing. AS3 DOES have this separate audio channells and mixer.

it shouldn't be seen as being a 'bug' , but rather an amazing add on of possibilities. i haven't tried it, but i think it is possible to be playing one sound ( in the first sound channel), then add another sound on top of it ( in a second sound channel), and then pan those channels, one sound in right speaker, the other in the left, you can add other channels too, and mix volumes, as well as other features, all of which i will look forward to exploring at some point.

... i'lllet you know what i find after i look at your files


Return to posts index

demetri tashie
Re: removeChild doesn't kill the audio in external swf (as3)
on Apr 20, 2009 at 4:32:04 pm

OK, so i just tested it using your files ( that had my code for the main SWF). the only thing i did different is to swap your sound ( "pop" ) for actual MP3s. it was impossible for me to determine a sound channel clearing from that short sound. i also changed the text within the movie clip in the external SWFs, to show which sound clip was playing.

also, your swf2 didn't have any audio (?), so i duped swf#1 files and changed them to #2, with a new text and mp3.

but once again- everything works, even with this 'delayed' sound.

the only glitch i see, which can be taken care of with some additional code, is that because of the 'delay' of those 60 frames ( 2 seconds), that if someone clicks on the first button before that movie clip reaches frame 60, then it will have instructions to load the audio twice,... and it will ! you can either turn off the event listener of button 1 if swf1 is loaded,with "if" statements, or by some other means. we can help you with that, too.

here is the link to the (revised) files

31_40testexternalswf.zip.
substitute your own song1.mp3 and song2.mp3


Return to posts index


Steve Parker
Re: removeChild doesn't kill the audio in external swf (as3)
on Apr 20, 2009 at 4:50:38 pm

ah! that "glitch" you mentioned is exactly what was my issue was (i just didn't fully realize the issue til now). when i was testing i was doing just what you described, i was changing pages before the delayed audio fired, which meant the audio was still being told to play.

think i got it covered from here! thanks!


Return to posts index

demetri tashie
Re: removeChild doesn't kill the audio in external swf (as3)
on Apr 20, 2009 at 5:06:36 pm

: - )

i am glad it makes sense now. here's my 2 unasked for cents on how to easily resolve that 'glitch' now:

1) the absolutely easiest way, in this example, is to place the bt1.addEventListener not in a 'Global scope'( where it is placed now,and accesable to all), but place it as the first line of code in
fucntion btnCLick2
so it only first becomes available if and when someone decides to hit button 2 - thus keeping it disabled until that point.

let us know how it finalizes for you .




Return to posts index

Koen Hertz
Re: removeChild doesn't kill the audio in external swf (as3)
on May 20, 2009 at 7:36:29 pm

You truely made my day!
thanx for the snippet!

Now i can go to bed...


Return to posts index


eamon white
Re: removeChild doesn't kill the audio in external swf (as3)
on May 4, 2012 at 9:15:28 am

Can u post How figure out this bug in now working on the same and I can seem to figure how to get past this glitch in would be a big help


Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2017 CreativeCOW.net All Rights Reserved
[TOP]