Youtube AS3 Wrapper v0.7.1
April 21st, 2009
A new version of Youtube AS3 Wrapper is available: v0.7.1
This new version adds Adobe AIR support.
The new version is available for download Youtube AS3 Wrapper – Downloads
Also, a new test harness player sources have been added, which makes use of the new wrapper and it’s AIR capabillities.
You can download the sources for the new Test Harness Player at the same link above.
Enjoy!


Sorry if this is a stupid question, but I’m not very good with AS. I have a php script that loads the player via normal html . In the php script I have variables that contain the ids for the video. I want to load the video with loadVideo(videoId, 0), but I’m not sure how to pass the videoId variable from php to AS. I tried using youtubeOD.swf?youtubeID=Nug2wVYDE6g from php.
If you would like to pass variables in flash like that you must handle them: url variables are received in “stage.loaderInfo.parameters”. If you pass a variable named “youtubeID”, you can access it in flash using stage.loaderInfo.parameters["youtubeID"]. So you can use directly player.loadVideo(stage.loaderInfo.parameters["youtubeID"]).
One more thing, you should use the “flashVars” parameter of the “” tag to pass in variables in flash when you embed the swf in html:
<object …..>
<param name=”flashVars” value=”youtubeID=Nug2wVYDE6g” />
….
</object>
If you use swfObject to dynamically embed the swf, you have the variables object parameter you can pass to the embedSWF function.
If you use the AC_FL_RunContent method, just add 2 more parameters to it, the first named “flashVars”, the second the value of the flashVars:
AC_FL_RunContent(……
….
“flashVars”, “youtubeID=Nug2wVYDE6g”
);
To pass more variables, separate them with &.
Hi,
Thank you very much for this class.. really what I need for my project now. I have a question though. I worked on my project based on your ‘How it works’ entry. My version calls on the player everytime I want to load a video. Do I need to load it everytime I need to play a video or I can just load it once and just call on loadVideo method. Right now I can only play 1 video and never be able to play again when I load it.
Help is much appreciated. Thanks!
Hi,
You are very welcome.
As for your question, the answer is no. You only need to load the player once, and call loadVideo method whenever you want to load and play a clip.
For mor information and entire documentation, please check the Youtube AS3 Wrapper page and follow the links there.
Welcome,
Ovidiu
@admin
Thanks for the quick reply. I still can’t seem to make it work. The code is inside my Flash IDE which I’m sure won’t matter. Below is the code (hope you dont mind) :
import pinosh.youtube.*
var video:String = “46izZS3qT4E”
var player:IYoutube;
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, playerLoaded);
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, playerCannotBeLoaded);
loader.load(new URLRequest(”ytPlayer.swf”));
function playNow():void { // FUNCTION CALLED INSIDE A MOUSE EVENT FUNCTION
addChild(loader)
player.loadVideo(video);
}
function playerLoaded(event : Event):void {
// grab the loader object
var loader:Loader = event.target["loader"] as Loader;
loader.x = fullPanel_mc.x + fullPanel_mc.vidHolder.x
loader.y = fullPanel_mc.y + fullPanel_mc.vidHolder.y
// add it to the display list of TestPlayer
//addChild(loader);
// grab the IYoutube object; it’s the content property of the loader object
player = loader.content as IYoutube;
// start listening for the events dispatched by player
player.addEventListener(YoutubeEvent.PLAYER_READY, onYTPlayerReady);
player.addEventListener(YoutubeEvent.PLAYER_STATE_CHANGE, onYTPlayerStateChange);
player.addEventListener(YoutubeEvent.PLAYER_ERROR, onYTPlayerError);
}
function playerCannotBeLoaded(event : IOErrorEvent):void {
// trace the for this test, bu should be handled more carrefully
trace(”Player Cannot be found: “+event);
}
function onYTPlayerReady(event : YoutubeEvent):void {
// now our player is ready to rumble; let’s play a clip
player.setSize(310, 233);
}
function onYTPlayerStateChange(event : YoutubeEvent):void {
trace(”TestPlayer::onYTPlayerStateChange ==> “+player.getPlayerState());
}
function onYTPlayerError(event : YoutubeEvent):void {
trace(”TestPlayer::onYTPlayerError ==> “+player.getPlayerError());
}
As you can see, it is only edited a bit. What shows after I upload and test it the YouTube logo appears but it doesn’t go on and play the video.
Hi again,
Your code is good, however there’s one small problem with it.
You commented out this line //addChild(loader); inside playerLoaded method, and added it in playNow():void method.
The wrapper starts its initialization when it is added to the display list. So, only in playNow the wrapper starts to initialize, and it is not ready when you call loadVideo (right after adding it to the display list).
What you should do, you should uncomment the line in playerLoaded method and comment it in playNow instead.
Also, check to see if the player is ready before calling player.loadVideo.
Here is how your code should look:
import pinosh.youtube.*
var video:String = “46izZS3qT4E”
var player:IYoutube;
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, playerLoaded);
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, playerCannotBeLoaded);
loader.load(new URLRequest(”ytPlayer.swf”));
function playNow():void { // FUNCTION CALLED INSIDE A MOUSE EVENT FUNCTION
if(player != null && player.isPlayerReady())
{
addChild(loader)
//player.loadVideo(video);
}
}
function playerLoaded(event : Event):void {
// grab the loader object
var loader:Loader = event.target["loader"] as Loader;
loader.x = fullPanel_mc.x + fullPanel_mc.vidHolder.x
loader.y = fullPanel_mc.y + fullPanel_mc.vidHolder.y
// add it to the display list of TestPlayer
addChild(loader);
// grab the IYoutube object; it’s the content property of the loader object
player = loader.content as IYoutube;
// start listening for the events dispatched by player
player.addEventListener(YoutubeEvent.PLAYER_READY, onYTPlayerReady);
player.addEventListener(YoutubeEvent.PLAYER_STATE_CHANGE, onYTPlayerStateChange);
player.addEventListener(YoutubeEvent.PLAYER_ERROR, onYTPlayerError);
}
function playerCannotBeLoaded(event : IOErrorEvent):void {
// trace the for this test, bu should be handled more carrefully
trace(”Player Cannot be found: “+event);
}
function onYTPlayerReady(event : YoutubeEvent):void {
// now our player is ready to rumble; let’s play a clip
player.setSize(310, 233);
}
function onYTPlayerStateChange(event : YoutubeEvent):void {
trace(”TestPlayer::onYTPlayerStateChange ==> “+player.getPlayerState());
}
function onYTPlayerError(event : YoutubeEvent):void {
trace(”TestPlayer::onYTPlayerError ==> “+player.getPlayerError());
}
Now it should work ok.
Test in browser or in standalone player, not inside Flash IDE.
Let me know how it is working.
Hi again,
Hmmm… in the additional if statement, there’s an addChild(loader) you also commented out player.loadVideo(video); Was it a mistake? If not, where can I call the loadVideo method now?
Thank you!
Ohh sorry,
That was a mistake
Uncomment player.loadVideo(video); and comment addChild(loader); instead.
Hope i didn’t make any other mistakes.
@pawikan
Really no luck in your class for multiple video loading. What I’m doing is I need to hide the video player when it’s not in use. I can only go as far as playing 1 video and then after that, it doesn’t work anymore.
Anyway, thanks for your time in replying and reviewing my post.
I really don’t understand what are you trying to do.
If you try to play 2 youtube videos in the same time, that’s not going to work.
However if you want to play multiple videos, one at a time, that’s straight forward, just call the load method anytime you want to play a video.
If you want to hide the player, just set the visible property of the loader object to be false.
When you need it again, set it back to true.
You can look at the code for the test harness player to see how it plays multiple videos when a title in the right list is selected.
@admin
It’s one at a time. Okay, let me try 1 more time. I know it’s really straight forward. I like how simple your class is implemented that’s why I chose to use this
One more thing, If I use destroyAndReload() does this remove the player/ loader instance? So I need to load it again? Cause this is how I close the videos
Thanks!
@pawikan
Also, if the player is not ready or isPlayerReady() returns false, is there a way to make it go ready and once it is, it’ll be able to load the next video on que?
Thanks!
OK.
First of all you should not use destroyAndReload(). This method destroys the player, so the player cannot be used anymore, and reloads it. So you’ll have to wait for the YoutubeEvent.PLAYER_READY event again, before you can load other videos. This method is deprecated however since v0.7. You shouldn’t use it.
You may call loadVideo even if a video is already playing. You don’t need to stop the video that’s playing, you may load another video directly.
However if you want to stop a video, you have the stopVideo() method.
isPlayerReady() returns true only after the YoutubeEvent.PLAYER_READY event is fired. So you should tie your code somehow to the onYTPlayerReady method.
If you call loadVideo when isPlayerReady() is false, nothing will happen, not even when the player becomes ready!
What to remember: don’t use destroyAndReload, listen for YoutubeEvent.PLAYER_READY, when fired, the player is ready to play your videos, call loadVideo to play videos, call stopVideo to stop videos (optionally), call loadVideo to play other videos… If you however call destroyAndReload, you must take this list from the beginning.
Finally! I’ve fixed the bug
The bug is not in your class but with Bitmap.draw() having a conflict whenever it Bitmap.draw() is applied to the whole flash movie and the player is already added (addChild()) to the Display list. Not sure why.
Thanks for your patience, dude. All the best.
Hi, Is there an option with this wrapper to switch between the chromeless and chromed youtube player? Thanks
Hi,
No this is chromeless player only.