Update: keep up to date with my latest code on my Github.
I’ve recently had an article on the YouTube Player API for Actionscript 3 published on the Flashtuts+ network. However, some people are having issues getting to grips with loading another video in-situ, so here’s a slight change to the code:
Once you read the tutorial, you will finish with two important files:
- YouTubePlayerWrapper.swf – The AS2 wrapper
- App.as – The AS3 class files
Now the first thing to do in this customisation is to change the name of “App.as” to “YouTubePlayer.as”, therefore our class is now called “YouTubePlayer”. I’ve also added two new public variables called “playerWidth” and “playerHeight”, so the class’s code is now like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | package { import com.gskinner.utils.SWFBridgeAS3; import flash.display.Loader; import flash.display.Sprite; import flash.events.Event; import flash.net.URLRequest; public class YouTubePlayer extends Sprite { public static const BRIDGE_NAME:String = 'YouTubePlayerWrapperBridge'; public var playerWidth:Number = 600; public var playerHeight:Number = 400; private var player:Loader; private var bridge:SWFBridgeAS3; private var videoId:String; private var loaded:Boolean; public function init(videoId:String):void { this.videoId = videoId; if ( !loaded ) { var request:URLRequest = new URLRequest( 'assets/swf/YouTubePlayerWrapper.swf' ); player = new Loader(); addChild( player ); player.contentLoaderInfo.addEventListener( Event.INIT, handlePlayerLoadedComplete ); player.load( request ); } else { handlePlayerLoadedComplete(); } } public function play(videoId:String):void { bridge.send( 'playVideo', videoId, playerWidth, playerHeight, false, null, false ); } public function stop():void { bridge.send( 'stopVideo' ); } public function handlePlayerLoadedComplete(e:Event=null):void { if ( bridge ) { handleBridgeConnect(); } else { bridge = new SWFBridgeAS3( BRIDGE_NAME, this ); bridge.addEventListener( Event.CONNECT, handleBridgeConnect ); } } public function sendEvent(e:String):void { trace( e ); } private function handleBridgeConnect(e:Event=null):void { loaded = true; play( videoId ); } } } |
Now we can control this class through another base class, so create a file called “App.as” and put this code in there:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | package { import flash.display.Sprite; import flash.events.Event; import flash.events.MouseEvent; public class App extends Sprite { private var player:YouTubePlayer = new YouTubePlayer(); public function App() { var button:Sprite = new Sprite(); addChild( player ); button.graphics.beginFill( 0xFF0000 ); button.graphics.drawCircle( 0, 0, 20 ); button.graphics.endFill(); button.addEventListener( MouseEvent.CLICK, handleClick ); addChild( button ); player.init( '76aG8wcb8V8' ); } private function handleClick(e:MouseEvent):void { player.init( 'U5A7AjsZHmQ' ); } } } |
All this class does is create a new “YouTubePlayer()”, it the draws a button, adds a listener that loads another video, thus allowing you to see how you can easily and simply load in a simple YouTube Player, like so:
PS: I am going to publish my full YouTube Player API Library for AS3 on github very soon!




Hi Ahmed!
I´m trying to use your source, but when i execute the swf (in flash), it´s give me de following error:
*** Security Sandbox Violation ***
SecurityDomain ‘http://www.youtube.com/v/f8nNOQOh7dc&autoplay=0&loop=0&rel=0&showsearch=0&hd=1′ tried to access incompatible context ‘file:///data/voraz/sua%5Fempreza/v2/public/flash/site/index.swf’
Can you help me?
Thanks
Posted: July 2, 2009 at 2:53 pm;Don’t worry, that’s not an error, read my post about security violations, that’ll tell you all you need to know.
Posted: July 2, 2009 at 3:11 pm;Thanks for this good post….
Posted: July 17, 2009 at 12:44 pm;“This video is not available in your country (Italy) due to copyright restrictions.” , damn YouTube
Posted: August 5, 2009 at 1:58 pm;You’re great Ahmed, thanks for your tutorials !
Glad I could help
Posted: August 5, 2009 at 8:31 pm;Hi, thanks again for you tutorial, here’s what I’ve made using it http://www.kongregate.com/games/locos/youkongregate
Posted: August 18, 2009 at 12:28 pm;Good work, glad you like it!
Posted: August 18, 2009 at 1:16 pm;HI Ahmed,
Posted: August 22, 2009 at 11:30 am;How can i dispatch events to controll the player or listen to events that the player dispatches like READY ect from the main app that holds the player?
You can do player.addEventListener and listen for the events in YouTubePlayerEvents. At the moment, you can only tell it to play, stop and resize! Is there anything else you’re after?
Posted: August 22, 2009 at 12:59 pm;Hey – thanks for the tutorial. It’s very helpful but I’m having a weird problem. I can load a player and play a YouTube video, but when I call player.init(‘youtubeid’) (same way you do with the button above) the new video loads and plays, but all the YouTube controls disappear. Any ideas?
Posted: August 24, 2009 at 12:56 am;Can I see your code and an example?
Posted: August 25, 2009 at 6:32 am;Howdy – I got sidetracked and couldn’t come back to this project for a while. But sample files can be found here…
http://mogeworks.com/youtubesample/sampleYouTube.zip
The first video that loads is an FLV. If you click the more tab, you’ll see three vids. The second two are YouTube. It loads fine the first time, but if you reload or load another YouTube vid, the controls disappear. I haven’t a clue where to even start looking for them. TabFeatured.as is where the code for the videos is, round about line 133.
Thanks for any help -
Moge
Posted: September 2, 2009 at 3:57 am;Ok try using the new code up on github and while you’re there checkout the latest wrapper
Posted: September 2, 2009 at 7:37 pm;Thanks! I’ll check that out.
Posted: September 3, 2009 at 9:52 pm;Any idea why my attempts at this are only playing the sound and not the video?
Posted: September 5, 2009 at 3:47 pm;Got any examples I can see?
Posted: September 5, 2009 at 10:34 pm;Hello Ahmed,
Thanks for your code. It’s working right, but is it possible to play two or more videos at the same time (and the same flash app of course) ?
Fylyps
Posted: September 7, 2009 at 9:22 am;Not at the moment, it’s because it uses local connection, and you’d need a second one for a second player and so on to Nth. It’s also bad for memory, but have no fear cos YouTube have something up their sleeves…
Posted: September 7, 2009 at 9:25 am;What do you mean? Are they going to release AS3 player eventually?
I’m asking you about that, because i’m currently working on the project which should has the ability to load several vids at the same time and it’s hard to find a way to do that.
greetings
Posted: September 7, 2009 at 10:00 am;I can’t comment on what YouTube are going to do I’m afraid!
If you need to load serveral ones, it may be easier to use JavaScript for the time being! Otherwise you’ll need several wrappers, each with their own unique local connection id.
Posted: September 7, 2009 at 11:42 am;Hello .. My name is Rodrigo. I have a very big doubt on that player on youtube in flash … I tried to adapt it to AS3 but I could not … Could you get me up and running in AS3?
Posted: September 7, 2009 at 5:37 pm;Hi Rodrigo, what issues are you having?
Posted: September 7, 2009 at 6:30 pm;my problem is to put the youtube api to work with AS3, to be honest I did not understand how the interaction of API with AS3, this tutorial you wrote. I would like to make the API work also putting a thumbs picture of other videos and you can click on them to make their video display. I hope you can help me.
Posted: September 8, 2009 at 1:26 pm;Ok, I’d suggest you read this: http://flash.tutsplus.com/tutorials/video/creating-a-youtube-search-and-play-gadget-with-puremvc/
Posted: September 8, 2009 at 1:29 pm;I liked this tutorial, but first I would like to make the API work with AS3, then I would study how to put the thumbs … as I said before, I found a little confusing the way you put the API to work with AS3 in the tutorial. If you can help me thank you.
Posted: September 8, 2009 at 3:03 pm;All you need to do to get the player to work is include the class at: com.firestartermedia.lib.as3.display.component.video.YouTubePlayer
And then load the player like so:
2
3
4
5
6
7
8
player.autoPlay = true;
player.wrapperURL = 'assets/swf/YouTubePlayerWrapper.swf';
player.play( 'AF4a-N4fAuI' );
addChild( player );
I did a test here and gave error …
Posted: September 8, 2009 at 3:33 pm;ReferenceError: Error # 1056: Can not create the property wrapperURL of YoutubePlay.
Please post your code, cos it looks like you’re not referencing it correctly.
Posted: September 8, 2009 at 3:44 pm;which code? class YoutubePlay?
Posted: September 8, 2009 at 4:08 pm;Yep. It looks as though the “YoutubePlay” class you’re using isn’t correct. Zip up your project, upload it and post a link. I’ll have a look for you.
Posted: September 8, 2009 at 4:58 pm;follow the link
Posted: September 8, 2009 at 5:36 pm;http://www.sendspace.com/file/25onrr
Ok, you’re doing in wrong. Firstly, you haven’t got the wrapper there and this is the code you need to use in your FLA:
2
3
4
5
player.init( 'AF4a-N4fAuI' );
addChild( player );
ok, and where I find the wrapper and how to use it? other questions, the class YoutubePlay I sent to you right?
Posted: September 9, 2009 at 3:36 am;You can download the wrapper from Github (http://github.com/ahmednuaman/YouTube-Player-Wrapper/blob/4324a0b29a10e30ae1399384d67014e399193ecd/YouTubePlayerWrapper.swf). I suggest you read the following posts and you can see lots of code examples: http://ahmednuaman.com/blog/tag/youtube/
Posted: September 9, 2009 at 6:19 am;The files in the Zip, are far managed to get … if you can fix them to work thanks.
http://www.sendspace.com/file/pkl75c
Posted: September 9, 2009 at 1:49 pm;Sorry Rodrigo, I’m not going to do your work for you! I’ve supplied you with the code you need to fix your FLA, so I’m sure that you can put it together…
Posted: September 10, 2009 at 10:00 am;Thanks for given this useful code. I will use it in my flv player.
Posted: September 23, 2009 at 12:03 pm;HI –
I’ve been off my YouTube project for a while and finally came back to it. I downloaded your latest .as code from github for YouTubePlayer and managed to track down the latst wrapper. However, I’m still having the same issue – when I load a second YouTube video, the YouTube controls disappear. Was there anything else I missed?
Thanks for you help –
Moge
Posted: September 27, 2009 at 8:08 pm;thank’s, that was nice!!!
Posted: September 27, 2009 at 8:48 pm;I have a problem, when you “compile” the youtubewrapper.as, what you put in the FLA file? to convert in SWF?
because in the milestones examples you pass the ID of a specific video…so?
sorry my english
Posted: September 28, 2009 at 4:09 am;Ok, try getting the latest wrapper: http://ahmednuaman.com/blog/2009/09/01/new-shiny-and-improved-youtube-wrapper/
Posted: September 28, 2009 at 1:47 pm;There’s no need to use the FLA, but don’t bother compiling it yourself, just use my wrapper: http://ahmednuaman.com/blog/2009/09/01/new-shiny-and-improved-youtube-wrapper/
Posted: September 28, 2009 at 1:48 pm;Hi ,
Posted: December 1, 2009 at 7:44 am;thats a wonderful stuff you wrote to play the you tube videos, its just amazing i have been playing with it for quite sometime now, but i just see only one issue with it, i see the CPU usage begin to increase as soon as video starts playing and if i keep playing videos continuously the CPU usage just goes on increasing, i know this could be a show stopper for this wonderful player, do you have any idea why this happens or any suggestions to get the usage down, i tried changing the frame rate of the flex app but it hardly works,
i appreciate your response.
Thanks again
Lisa
What you could do now is migrate to the new AS3 API: http://ahmednuaman.com/blog/2009/10/24/introducing-the-native-actionscript3-youtube-player/
Posted: December 2, 2009 at 3:41 pm;