Develop with the Official YouTube Chromeless Player AS3 API

A few days ago, Google released the official YouTube Chromeless Player AS3 API that gives us an exciting perspective of building flash applications integrating seamless Youtube videos.

Check out their announcement and the YouTube ActionScript 3.0 Player API Reference

View DemoDownload Source

Now let’s see a simple example that integrates a YouTube video into an ActionScript 3 Flash application with this API.

1. Create a new flash file (actionscript 3) and save it as youtube.fla. Set the stage size to 640×360 pixels.
Rename “layer 1 ” to “actions” and open the actions panel.

2. Following the YouTube ActionScript 3.0 Player API Reference, copy and paste the following code:

import flash.system.Security;

Security.allowInsecureDomain("*");
Security.allowDomain("*");

// This will hold the API player instance once it is initialized.
var player:Object;

var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.INIT, onLoaderInit);
loader.load(new URLRequest("http://www.youtube.com/apiplayer?version=3"));

function onLoaderInit(event:Event):void {
    addChild(loader);
    loader.content.addEventListener("onReady", onPlayerReady);
    loader.content.addEventListener("onError", onPlayerError);
    loader.content.addEventListener("onStateChange", onPlayerStateChange);
    loader.content.addEventListener("onPlaybackQualityChange", onVideoPlaybackQualityChange);
}

function onPlayerReady(event:Event):void {
    // Event.data contains the event parameter, which is the Player API ID 
    trace("player ready:", Object(event).data);

    // Once this event has been dispatched by the player, we can use
    // cueVideoById, loadVideoById, cueVideoByUrl and loadVideoByUrl
    // to load a particular YouTube video.
   player = loader.content;
   player.loadVideoById("nxrmJtaZBA0");
   player.setSize(640, 360);

}

function onPlayerError(event:Event):void {
    // Event.data contains the event parameter, which is the error code
    trace("player error:", Object(event).data);
}

function onPlayerStateChange(event:Event):void {
    // Event.data contains the event parameter, which is the new player state
    trace("player state:", Object(event).data);
}

function onVideoPlaybackQualityChange(event:Event):void {
    // Event.data contains the event parameter, which is the new video quality
    trace("video quality:", Object(event).data);
}

We display the video by calling the loadVideoById(videoId:String) method that loads and plays the specified video.

3. That’s it, test the movie to see it in action.

24 thoughts on “Develop with the Official YouTube Chromeless Player AS3 API

  1. dream

    very usefull but i when i publish it i get some error

    player ready:
    player state: -1
    video quality: medium
    Erreur d’ouverture de l’URL ‘http://www.youtube.com/get_video?noflv=1&video_id=uLEMqN-Za_U&splay=1&fmt=5&el=embedded&ps=chromeless&t=vjVQa1PpcFN9g5HaXK2rOwUO68ExK62CmwBShAOtis4=’
    player state: 3
    player state: 1
    Erreur d’ouverture de l’URL ‘http://video-stats.video.google.com/s?st=0.067&md=1&sd=BD075FE85 &fexp=903900,904510&ps=chromeless&cfps=0&bd=17424&bc=19472&len=601&hbd=6766308&ns=yt&hbt=7.151&plid=&splay=1&scoville=1&h=360&bt=0.328&docid=uLEMqN-Za_U&w=480&rt=1.211&vid=_LO0hpCkOM-jfg_3mTXcofS6u7mu_SgkC&fv=WIN 10,0,2,54&el=embedded&et=0.067&fmt=5’
    Erreur d’ouverture de l’URL ‘http://s2.youtube.com/s?st=0.209&vid=_LO0hpCkOM-jfg_3mTXcofS6u7mu_SgkC&ctp=1&yttk=1&ps=chromeless&docid=uLEMqN-Za_U&el=embedded&et=0.209&ns=yt’

    and some ID doesnt work

    ID of the video i used : uLEMqN-Za_U

    How do i do into have my swf exported ? when i compil it and i play my swf out of flash i have no video which playing but those error

    SecurityError: Error #2028: Le fichier SWF local-système de fichiers file:///C|/Documents%20and%20Settings/Propriétaire/Bureau/MYSPACE%20ALL/Mon%20myspace/youtube.swf ne peut pas accéder à l’URL Internet http://www.youtube.com/apiplayer?version=3.
    at flash.display::Loader/_load()
    at flash.display::Loader/load()
    at youtube_fla::MainTimeline/frame1()

    thanks

  2. Pingback: 45+ Advanced Adobe Flash Actionscript Trainings | Master Design

  3. DigitalDuane

    Seek and yea shall find! This is an incredible. You just saved me days of anquish!
    Thanks you very much!

    Digital D

  4. Pingback: Pambaa – Develop with the Official YouTube Chromeless Player AS3 API

  5. Tim

    awesome thanks!

    One question… i’m trying to do is to allow a user to paste any youtube video URL into the input text field and when the user presses a button the video starts to play

  6. Pingback: Advanced Adobe Flash Actionscript Trainings « Flash Criminals

  7. Giuseppe

    You should change the youtube video ’cause the one you have is restricted, it does not show up in Italy.

  8. admin Post author

    @ GIUSEPPE: Hi, thanks for your remark, we’ve changed the video.
    Indeed the old one has been restricted since.
    Thanks again.

  9. Pingback: The Best of 2009 in Flash Web Design - Flash Web Design and Design Photography | DesignOra

  10. Tony Hancock

    I have done this and it works perfectly, but i was wondering if it was possible to have this on a different frame other than frame one. I have tried it but the video just won’t play then.

    Any help is highly appreciated.

  11. Lee

    Hello,

    I’m not clear as to whether or not the new AS3 API still requires Javascript like the AS2 version did.

    lee

  12. Rob

    Hi, this is the first time that I have been able to get YouTube integrated into my flash site. I took your formula and placed it in one of my pages, adjusted the x and y coordinates, now when you go to that page it plays automatically. Now my problem is getting it to stop. What code do I use to have the movie stop when the user clicks another frame?

    Rob.

  13. Pingback: Free I Share 分享资源 分享快乐 » Blog Archive » 45个高级Flash Actionscript应用

  14. Pingback: Create a 3D Flipping Youtube Player | Web design studio

  15. Pingback: 45+ Advanced Adobe Flash Actionscript Trainings - Flash24h.com | Thế giới Flash của bạn!

  16. eamonn finn

    does anyone know why Im getting a blank screen then an output message saying
    Error opening URL ‘http://www.youtube.com/apiplayer?version=3’
    Error #2044: Unhandled IOErrorEvent:. text=Error #2035: URL Not Found.

  17. PIT

    Great work!!
    thanks!

    One more question… i’m trying to do a function that stop the video (and sound in background)
    I could remove the instance but the sound keep playing…

    The YouTube ActionScript 3.0 Player API Reference tells us to use player.destroy() ???

  18. Sab

    Hi!

    Awesome, thx!

    Is it possible to play private videos? Can I pass some account credentials to get a private video running?

    Any help is appreciated! Thx

    Sab

  19. Pingback: 45+ Advanced Tutorials of Adobe Flash ActionScript « CSS Tips

  20. Pingback: 45+ Advanced Tutorials of Adobe Flash ActionScript | JS Tips

Leave a Reply

Your email address will not be published. Required fields are marked *