The new YouTube AS3 Chromeless API in Flex, Air, Flash
I was very pleased that YouTube released a ActionScript 3 API for their Chromeless Player. I looked at some availabe code snippets on different blogs and unfortunateley, I had to realize that the API is not really object-oriented and a little bit annoying to work with (because of the missing code completion).
My current project - an independent Web Video Player - is still using the old workaround YouTube API Tubeloc and I have to change this. That is the reason why I created a Flex Component for the new YouTube AS3 API. If you would like to use my code for your Flex or Air projects, please feel free to use it and if you will find some bugs, please leave a comment!
Now I will start with some explanations of my code. I create 4 classes, which manage the whole YouTube Chromeless API. The class YouTubeAs3 contains all the most important functions and is based on the Flex Framework(!). The classes AirYouTube and FlexYouTube extends the YouTubeAs3 class. Both classes are Flex Framework based classes. I had to divide the YouTubeAs3 class into these 2 classes, because the Adobe Air environment don't support the command Security.allowDomain() and it is cleaner to use this command for a web-based projects. So therefore please use the FlexYouTube component for web-based projects and the AirYouTube component for your Air projects. The fourth class FlashYouTube is an only Flash plattform based class, that should be very handy to use for non-Flex projects.
The other classes just simplify the coder's life. So I created some static classes for the various video quality parameters (YouTubeVideoQuality Class), the various PlayerStates (YouTubePlayingState), the YouTube errors (YouTubeError) and of course the available YouTube events (YouTubeEvent).
I wrote an example application for you, which should be very easy to understand. In this very small Flex application you can stop, play and pause the YouTube videos:
//---------------------------------- // YouTube Player Control //---------------------------------- private function handlePlay(event:MouseEvent):void { youTubePlayer.playVideo(); this.addEventListener(Event.ENTER_FRAME,handlePlayingTime); } private function handlePause(event:MouseEvent):void { youTubePlayer.pauseVideo(); this.removeEventListener(Event.ENTER_FRAME,handlePlayingTime); } private function handleStop(event:MouseEvent):void { youTubePlayer.stopVideo(); this.removeEventListener(Event.ENTER_FRAME,handlePlayingTime); } // frame-based Intervall for showing loading and playing process private function handlePlayingTime(e:Event):void { labelTime.text = youTubePlayer.getCurrentTime() + " / " + youTubePlayer.getDuration(); progresPlayingVideo.minimum = 0; progresPlayingVideo.maximum = youTubePlayer.getDuration(); progresPlayingVideo.setProgress(youTubePlayer.getCurrentTime(),youTubePlayer.getDuration()); progressLoadVideo.minimum = 0; progressLoadVideo.maximum = youTubePlayer.getVideoBytesTotal(); progressLoadVideo.setProgress(youTubePlayer.getVideoBytesLoaded(), youTubePlayer.getVideoBytesTotal()); }
Some behaviours of this Flex-based YouTube component is adjusted for the Flex Framework. If you would like to change the size of the YouTube Player, you can use the witdh and height properties of the component. Furthermore there exists some other properties:
[Bindable] public var volume:int; // Value between 0 and 100 [Bindable] public var playerState:Number; // Read-only!!!! [Bindable] public var playbackQuality:String; // Use the static variables of the YouTubeVideoQuality class
Handling and managing of the YouTubeEvents are the last thing I would like to explain. I support the same 4 events, which the YouTube API supports. The only difference is that I wrapped the data of the events in a special YouTube Event for an simpler event handling and code completion.
[Event(name="youtubeStatus", type="de.derhess.video.youtube.YouTubeEvent")] [Event(name="youtubePlayerLoaded", type="de.derhess.video.youtube.YouTubeEvent")] [Event(name="youtubeError", type="de.derhess.video.youtube.YouTubeEvent")] [Event(name="youtubeVideoQuality", type="de.derhess.video.youtube.YouTubeEvent")]
So here I is my code of my app. I think it is very self-explanatory...
/* Here should be some code but wordpress always crashed
because of parsing problems... Please download the code
and look at line number 23-112 in the src folder the file
flex_youTube.mxml */
The rest of the functions contains exactly the same functionality as the YouTube API. If something ambiguous, please look at the YouTube API documentation or leave a comment. THX!
Download the Flex YouTube components and source code
Related posts:


hey man, thanks for the classes,
one question though: is there a way to destroy the player??
Thanks so much for this! I went about creating an interface for the youtube api as soon as it came out, but I see you beaten me too it (and added so much more as well!).
Many thanks again, have you thought of submitting this to google? I think they need help with this kind of thing.
@munnaz1
yes, you can destroy the player. Look at the example app. There is a button with the label “destroy”. Click this button and the player instance should be destroyed…
@enzuguri
so now you can spend your time for other important things
Yeah, it is a little bit sad, that google have not done by themself… the same is valid for the vimeo api team… I think they are not very interested in the independent or small Flash / Flex developer life. Anyway, I am happy that they provide such a kind of APIs for us.
Hey, I have not beaten you. We are one community!
Ah,.. genau was ich gesucht habe. Darf ich deinen Code für ein opensource projekt benutzen?
Hi FJ,
du kannst meinen Code gerne für dein Open Source Projekt benutzen. Wäre cool wenn du irgendwo n kleinen Verweis machen könntest, dass du meine Klassen benutzt hast. So quasi die eine Hand wäscht die andere
Grüße aus Linz
Flo
thanks. Natürlich. Werde dir bescheid geben sobald ich soweit bin.
http://github.com/FlashJunior/OSFlashVideoPlayer
wer mithelfen will ist herzlich willkommen
I resized the application and tried to put youtube:FlexYouTube inside a canvas to position it, but it is always half off the screen and to the upper right. How do I position it?
Hi Sam,
I tried to reproduce your bug, but I had no success. Could you please post your code? I tried almost everything with the FlexYouTube Component inside a Canvas and it works fine for me… Thx
Hi,
i have try your project in a new air application and it is working fine.
But i want to intergrate this player in a bigger application, so i have added the player to my components, changing the “application” tag in a spark “group”.
I have a sanddbox violation when running it : [SWF] /apiplayer – 3 724 octets après la décompression
Warning: Domain s.ytimg.com does not specify a meta-policy. Applying default meta-policy ‘master-only’. This configuration is deprecated. See http://www.adobe.com/go/strict_policy_files to fix this problem.
[SWF] /yt/swf/apiplayer3-vfl155676.swf – 171 820 octets après la décompression
[SWF] /[[IMPORT]]/s.ytimg.com/yt/swf/apiplayer3-vfl155676.swf – 1 927 octets après la décompression
*** Security Sandbox Violation ***
SecurityDomain ‘http://www.youtube.com/apiplayer?version=3′ tried to access incompatible context ‘app:/Potati.swf’
After the error warning, it is working fine…
If i try to add in the mxml file of your player :
Security.allowInsecureDomain(“*”);
Security.allowDomain(“*”);
, i have a security error :
SecurityError: Error #3207: Application-sandbox content cannot access this feature.
at flash.system::Security$/allowInsecureDomain()
I do not know how to put the player in another sandbox of my air app…
Do you have an idea on how to solve this issue ?
Thx
I was wondering if you could point me the direction of learning how to implement your code in Flash?
I am a total Newbie and just started to use Flash. I created an Indesign document and exported it to Flash and now want to add YouTube Movies. But I can’t figure it out. I thought I would load a component but I am totally lost.
If you can give me some clues I would appreciate your assistance.
Robert
Hi Robert,
I don’t know how InDesign deals with Flash Content. But in Flash you can write:
import de.derhess.video.youtube.*;
function handlePlayerLoaded(event:YouTubeEvent):void
{
// Play the VideoID you want
youTubePlayer.cueVideoById(“jAHYJX4-glU”,0,YouTubeVideoQuality.DEFAULT);
}
var youTubePlayer:FlashYouTube = new FlashYouTube();
// Add and check if the YouTube Player API is loaded
youTubePlayer.addEventListener(YouTubeEvent.PLAYER_LOADED,handlePlayerLoaded);
//Add your YouTube Player to the rendering List
addChild(youTubePlayer);
I hope this code snippet helps.