Loading / unloading youtube videos in flash
Loading / unloading youtube videos in flash
Hi dear readers, I recently loaded youtube videos in a flash project, it was quite straight forward, but however there was a issue when unloading the videos, cz if you donot unload the movie, it will keep playing and its sound will keep coming out, despite the fact you transfer control to anywhere at the movie.
So, I searched the forums, but I could’nt found the solution. Lots of guys are facing this issue. So, here is the solution:
// The first thing to load any external MC,
// is to create an instance of movieclipLoader Class.
var video_mcl:MovieClipLoader = new MovieClipLoader();
// Now you will like to create a container where the MC will actually load.
this.createEmptyMovieClip(”container”,this.getNextHighestDepth());
// Create a listener object
var mclListener:Object = new Object();
// add it to the movieClipLoader object
video_mcl.addListener(mclListener);
// onLoadInit will be triggered when the external MC is ready. So its nice place to set new MC dimensions etc.
mclListener.onLoadInit = function(target_mc:MovieClip) {
// set dimensions or anything..
}
// now assign youtube’s movie id and create complete URL to load from.
utube_id = “UfMglbWyX4I”;
full_address = “http://www.youtube.com/v/” + utube_id;
//eventually use loadClip function
video_mcl.loadClip(full_address, container);
Now your youtube MC is loaded and its playing by default. Now you have to unload it or close it.
The first thing that will come to flash developer’s mind isĀ movieClipLoader’s class unloadClip function:-
i.e:
video_mcl.unloadClip(container); // but unfortunately that doesnot work to unload youtube’s MC.
Secondly, you can try removing the container MC which contains the loaded content
i.e:
container.removeMovieClip() // but unfortunately you cannot explicity remove the container MC.
so last but not the least, you will try to atleast stop the sound and will use global function, i.e:
stopAllSounds(); // but that will work for the FLASH IDE, but after publishing it wont hav any affect..
So here is the tip, when you load the youtube MC in your container, then your container reacts as a youtube player.. Infact its the youtube player now and you can control all of youtube’s api functions available with that container object..
So simply doing:
container.stopVideo(); // will stop the video and its sound.
you can use all other functions with the container instance as well..
Hope it helps to all who got stucked with this issue..
cheers
Jawad.

Social Links