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.
9 Comments
Doesn’t actually unload the MovieClip, though. This is has the same problem as everything else we’ve tried.
BUTTON ONE or BUTTON TWO will load no problem. But, that’s not the problem we’re trying to solve. UNLOADING either movie once it’s loaded is impossible, even with your new code provided above.
Test it yourself:
// MAIN MOVIE:
var video_mcl:MovieClipLoader = new MovieClipLoader();
this.createEmptyMovieClip(“container”,this.getNextHighestDepth());
var mclListener:Object = new Object();
video_mcl.addListener(mclListener);
mclListener.onLoadInit = function(target_mc:MovieClip) {
target_mc._xscale = 78.5;
target_mc._yscale = 78.5;
// BUTTON ONE:
on (press) {
container.stopVideo();
utube_id = “nhwu0hO0hnY&hl=en&fs=1&ap=%2526fmt%3D18&enablejsapi=1″;
full_address = “http://www.youtube.com/v/” + utube_id;
video_mcl.loadClip(full_address, container);
}
// BUTTON TWO:
on (press) {
container.stopVideo();
container.removeMovieClip()
utube_id = “gw40iv6C35c&hl=en&fs=1&ap=%2526fmt%3D18&enablejsapi=1″;
full_address = “http://www.youtube.com/v/” + utube_id;
video_mcl.loadClip(full_address, container);
}
I am trying to build a Flash website for my nephew at the above location. If you look at main page, the small photos link to larger photos using a carousel (you can also go to the “ASAVIDS” link at the top. What I’m trying to do is set up the large photos as playable videos from Youtube, rather than as links TO videos on YouTube. Any suggestions? I’ve tried several workarounds, but even when I get a video to load in the Flash, I get a “sandbox violation”.
not working……..its stops in first frame..i get the following error mesage in flash ide output panel…
SecurityDomain ‘http://s.ytimg.com/yt/swf/cps-vfl62057.swf‘ tried to access incompatible context ‘http://www.youtube.com/swf/l.swf?swf=http%3A//s.ytimg.com/yt/swf/cps-vfl62057.swf&video_id=_PnWw6m3McQ&rel=1&showsearch=1&eurl=http%3A//www.youtube.com/v/_PnWw6m3McQ&iurl=http%3A//i4.ytimg.com/vi/_PnWw6m3McQ/hqdefault.jpg&sk=mkpn9P_SnQyp0yRjAo1yKy3p68yFx1OiC&use_get_video_info=1&load_modules=1&hqt=1‘
*** Security Sandbox Violation ***
Connection to null halted – not permitted from http://s.ytimg.com/yt/swf/cps-vfl62057.swf
– Remote SWFs may not access local files.
*** Security Sandbox Violation ***
Connection to null halted – not permitted from http://s.ytimg.com/yt/swf/cps-vfl62057.swf
– Remote SWFs may not access local files.
Thanks for the helpful info here. I was pretty stumped when I found out I was unable to remove or unload the Youtube video, although I was able to modify the x and y position of the movieClip with Actionscript.
Using stopVideo() worked so at least I understand how the control the Youtube video better.
John
Hello, if you have the time, could you please post the full srcipt for loading youtube onto as3? All tutorials I’ve found are really complicated and this one seems pretty simple but I can’t make it work. Thanks a lot.
Thank you for this – been tearing my hair out for days! Worked a treat
Sorry guys, I have been away from my blog for quite a long time.. Now am back….. Thanks everyone for posting comments..
@nate
Actually the way I suggested does the trick to use youtube videos in your flash movies. removeMovieClip() or unloadclip functions doesnot have any effect over youtube video container. Its just a hint that how easily youtube API functions become available to you.
@Sajil and @Loydster
you should use: System.security.allowDomain(”http://www.youtube.com”);
cz we have to use youtube player’s functions.. so since both swfs are in different domains, i mean your swf at your domain.com and youtube player at youtube.com.. so allowDomain is required to access other swf’s variables and functions.
@gente21: man, i cannot promise.. but i would love to do so, if i get sometime during next few days.
@Dan and @John:
Great!.. its my pleasure..
Tks for de help.