unload dynamically loaded swf after displaying it AS3 - actionscript-3

I am doing a presentation on which I need to use a lot of video clips. I load all these videos dynamically using Loader. I use a code snippet like this:
var req:URLRequest = new URLRequest("video.swf");
var a:Loader = new Loader();
a.load(req);
stage.addChild(a);
a
Now the issue is: When I get to say the 7th one, it starts lagging. I do not know why, but I think it is because everything is loaded to memory. Is there a way I can erase a video from memory after displaying it? Or is there another solution to this?

If you just load a new video each time and add it to the stage, you will have a rather big hierarchy of movies sitting on top of each other; all kept on stage and in memory.
If you keep a reference to the previously loaded movie, you can just remove it from the stage when the 'next' movie is loaded. Haven't tested this properly, but just to give you an idea of what the code might look like:
var oldLoader:Loader = null;
var a:Loader = new Loader();
a.contentLoaderInfo.addEventListener(Event.COMPLETE, loader_complete);
a.load(<your url>);
function loader_complete(e:Event):void {
var newLoader:Loader = e.currentTarget.loader as Loader;
stage.addChild(newLoader);
if(oldLoader != null) {
stage.removeChild(oldLoader);
oldLoader = null;
}
oldLoader = newLoader;
}

I think I found the solution. If you use removeChild, it only stops displaying, but the whole thing is still in memory (cache). So the more you load the videos, the more you fill up the cache. What we need is something that completely clears the cache before loading another video.
I used unloadAndStop() and it seem to have completely done the trick, unless someone has something against it.
Thanks again

Related

As3 - I Simply cannot remove loader child no matter what

I have loaded an external SWF to my timeline using a common loader method. The SWF is Jwplayer and it is polling a live rtmp stream. Here is the snippet I used to load it successfully.
var c4:Loader = new Loader();
var c4req:URLRequest = new URLRequest("player.swf");
var c4flashvars:URLVariables = new URLVariables();
c4flashvars.streamer = "rtmp://myserver";
c4flashvars.file = "live"
c4req.data = c4flashvars;
c4.load(c4req);
addChild(c4)
Now, the player loads fine. It begins to poll a live stream, too. However, no matter what I try I cannot remove this child from the stage. I have tried the following:
removeChild(c4);
c4.unloadAndStop();
For testing, I am calling these from a timer/event listener/function combo. I traced it to make sure it was being fired, and it is. However the player just sits there on the stage and refuses to leave. Output isn't throwing any errors. I half suspect that because it's polling a live stream that the loader never sees the external SWF as fully loaded. But that's just a hunch.
The overall scope of this little project is to load the player, check for an event, and re-load the player (unload, and reload). If anyone could help me understand this, get past this, or is feeling generous enough to show me the light; I'd appreciate it greatly. I've put a solid day into messing around with this and searching. I'm just not grasping something. Thanks!

parent.AddChild not working after protecting swf using SWFProtection!

I usually, use SWF Protection, to protect my swf's from decompiling.
It also adds a load bar to the swf.
The problem is that I have codes like this:
logo= new marca();
parent.addChild(logo);
logo.mouseEnabled=false;
I use parent, because I need to put the movie clip above everything, because my application allows the user to add a lot of things to the stage, so a don't want anything covering the logo.
The application works well while unprotected, but If I protect it using SWF Protection, then, I get just white screen. Nothing appears after loading.
Is there another way, to put a movieclip above everything, without being necessery to add a ON ENTER FRAME LISTENER, to update the movieclip deph, to keep it above everything?
You can use addChildAt instead of addChild to add your other clips to the stage under the logo's level.
var levelUnderLogo:uint = numChildren == 1 ? 0 : numChildren-2;
clipYouWantUnderTheLogo = new MovieClip();
addChildAt(clipYouWantUnderTheLogo, levelUnderLogo);
edit: You can also put every other clip inside another clip that is always under the logo.
var base = new MovieClip();
addChild(base);
var logo = new Logo();
addChild(logo);
// Add all your other objects into the "base" clip.
var someClipUnderTheLogo = new MovieClip();
base.addChild(someClipUnderTheLogo);
You are getting the white screen because the swf embed method is breaking in the javascript. Basically you have a javascript error.
Because you did not post code I can't help you much more. All I can say is put alerts in the javascript and see how far you get.

Is there a way to use a sound channel on a movieclip?

The only way i can describe this is showing the code I have already then trying to explain what i want to do..
Basically, I am creating a soundboard game, where at the bottom of the screen I will have a bar which is a movieclip, and I will be dragging other movieclips onto it and then clicking pay, and using an array and .push they will play in order. I am trying to put the sounds onto the movieclips using code. So far, I have this:
var snd1:Sound = newSound();
snd.load(newURLRequest("naturefrog.wav"));
var channel:SoundChannel;
snd.addEventListener
I am now stuck with what I would put for the listener to listen for.
See this answer for knowing when sounds have finished loading, if the sound is external:
Loading a sound from an external source
Now once you're ready to play the sound and listen for when it completes, that's answered here:
demonstrates setting up listeners for sound_complete, as well as looping
You should use the Flash API for Sound to determine what listeners to add. There are some helpful examples at the bottom of the page.
I assume you'll want to add:
snd.addEventListener(Event.COMPLETE, completeHandler);
snd.addEventListener(Event.ID3, id3Handler);
snd.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
snd.addEventListener(ProgressEvent.PROGRESS, progressHandler);
You are pushing the sound reference of a movieclip into an array when user drag it into a particular movieclilp, right? If you do so you can shift an element from array(array.shift, usually this removes and returns the first element of the array).
function playSoundInArray(){
if(array.length){
var snd:Sound = array.shift() as Sound;
channel = snd.play();
channel.addEventListener(Event.SOUND_COMPLETE, onSoundPlayed);
}else{
//Do something here if there is no sound reference or the array is empty
}
}
function onSoundPlayed(e:EVent){
channel.removeEventListener(Event.SOUND_COMPLETE, onSoundPlayed);
playSoundInArray();
}

How do I include flashvars in AS3?

I am brand new to AS3 and I am trying to move all of the data from an html file, into a swf.
The old html file contained this coding:
<EMBED src="https://www.pandora.com:443/radio/tuner_9_2_0_0_pandora.swf" WIDTH="640" HEIGHT="528" FlashVars="_tunervar_shost=www.pandora.com">
So far from what I have learned I can use the loader, to substitute for the src. So in my swf, I have
var myLoader:Loader = new Loader();
addChild(myLoader);
var url:URLRequest = new URLRequest("https://www.pandora.com:443/radio/tuner_9_2_0_0_pandora.swf");
myLoader.load(url);
What I can't figure out though, is how to change the FlashVars from html into AS3. I have done a bit of research and from what it sounds like, I have to use something called root. I am completely lost on how to use this, and I am not even sure if this is the correct way to script it. Can someone help explain this to me?
One other problem I am having, is for some reason, the swf I am loading is blurry. Is there any way to fix this?
Try appending them to the asset that you're loading:
var url:URLRequest = new URLRequest("https://www.pandora.com:443/radio/tuner_9_2_0_0_pandora.swf?_tunervar_shost=www.pandora.com");

Audio of a specific video swf doesn't stop when I load another URLRequest

I am trying to stop the audio of the video swf and I can't get it to stop. Here is my code for loading the file:
var myLoader:Loader= new Loader();
myLoader.x=420;
myLoader.y=200;
// boolean variable set for use below in our function
var screenCheck:Boolean = false;
//These three linces keep stafe elements the same size, so they don't distort
var swfStage:Stage = this.stage;
video_btn.addEventListener(MouseEvent.CLICK,contentvideo);
function contentvideo (event:MouseEvent):void{
myLoader.load(new URLRequest("prevideo.swf"));
addChild(myLoader);
movie_btn.stop();
movie_btn.visible=false;
}
Now I have other functions that load different URLRequest and when they are loading, the audio keeps playing. Do I have to add a line of code to them? I also have an MP3 player and tried SoundMixer.stopAll(). I still need the mp3 player to keep playing.
I'm not familiar with what you're doing but just looking at the code and given the problem you're experiencing I wonder if that
addChild(myLoader);
has anything to do with it. It seems like the kind of thing that could easily create multiple child objects which is why you continue to experience the sound play back. Is there a removeChild[0] option or something?
A shot in the dark I know but I thought I'd offer the possibility anyway.