Develop with the Official YouTube Chromeless Player AS3 API

October 25th, 2009 in Flash

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.

  • Digg
  • del.icio.us
  • Reddit
  • StumbleUpon
  • Twitter


What you think ...

(14 Comments)

+ Add a Comment
  1. dream
    Oct 29th, 2009

    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. JIn
    Oct 29th, 2009

    Thanks, that will be helpful for my future work

  3. 45+ Advanced Adobe Flash Actionscript Trainings | Master Design
    Nov 1st, 2009

    [...] Develop with the Official YouTube Chromeless Player AS3 API [...]

  4. DigitalDuane
    Nov 3rd, 2009

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

    Digital D

  5. Pambaa – Develop with the Official YouTube Chromeless Player AS3 API
    Nov 11th, 2009

    [...] VIEW TUTORIAL Posted in 2D – Graphics, Adobe Flash, video player on Nov 11 by Billie Bongo | Print No comments Entry Details: This entry was posted on Wednesday, November 11th, 2009 at 10:22 pm, by Billie Bongo, and is filed under 2D – Graphics, Adobe Flash, video player. You can follow any responses to this entry through the RSS 2.0 feed. You can skip to the end and leave a response. Pinging is currently not allowed. Print This! [...]

  6. Tim
    Nov 25th, 2009

    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

  7. Advanced Adobe Flash Actionscript Trainings « Flash Criminals
    Nov 26th, 2009

    [...] Develop with the Official YouTube Chromeless Player AS3 API [...]

  8. Giuseppe
    Nov 29th, 2009

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

  9. admin
    Dec 1st, 2009

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

  10. kamal98
    Dec 10th, 2009

    This is great just what i needed. Hope it works the way i think.

  11. The Best of 2009 in Flash Web Design - Flash Web Design and Design Photography | DesignOra
    Dec 30th, 2009

    [...] Develop with the Official YouTube Chromeless Player AS3 API link [...]

  12. Tony Hancock
    Jan 8th, 2010

    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.

  13. Lee
    Jan 26th, 2010

    Hello,

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

    lee

  14. Rob
    Feb 23rd, 2010

    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.

Leave a Comment

Name (required)

E-mail (required, will not be published)

Website