Embed youtube video using AS3

Actionscript 3 | Posted on Mar 08 2010

This is a quick way to embed youtube videos into Flash.

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

var loader:Loader = new Loader();

var context:LoaderContext = new LoaderContext();
context.checkPolicyFile = true;
context.securityDomain = SecurityDomain.currentDomain;
context.applicationDomain = ApplicationDomain.currentDomain;

loader.contentLoaderInfo.addEventListener(Event.INIT, onLoaderInit);
loader.load(new URLRequest(""));

function onLoaderInit(event:Event):void {
    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 {
    // 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;
    // Set appropriate player dimensions for your application
    player.setSize(480, 360);

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

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

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

17 Responses to “Embed youtube video using AS3”

  1. Nice comment text. I can’t see a damn thing I’m typing. Anyway, I’m trying to embed youtube in a flash file, and not having much luck. There seems to be no tutorials that I can find after googleing for 2 days

    Any ideas where to start?

  2. How do you position?howhow do you position?

  3. Thanks very much for this, worked a treat.

    To dfgmark, you can position it by placing this in the actionscript, just after player.setSize(600, 254);:

    player.x = 0
    player.y = 45;

    the numbers represnting the co ordinate at which you want it placing.

    And yes, please change your comment text colour. Can’t see a thing.

  4. how do to remove player object or cancel.

    is possible?

    thank you

  5. use loader.unloadAndStop();

  6. Works great but where do you put the loader.unload in script

  7. hey i hav been trying but i cant seem to get the player to move around or rescale even if i change the values. is anyone else having this problem? I changed the pixels to 1920,1080 and the position to 400,500 but still nothing changes at all. help!!

  8. were you able to get the player to change so? if so how did you do it? im very stuck. Because i changed the setSize values and it will not change!!!!

  9. when i use this code the player works fine, but when i leave that page to go to another page on the site, the audio is still going. and if i go back to that page and hit play again, there’s 2 audio streams playing. is there a way to kill the sound from just that page and not the whole site? becuz there’s other sound effects i dont wanna get rid of.

  10. How do you allow the video to play in full screen?

    Check it out, my player does not have a full screen option? Please help.

  11. I am having the same problem as Rick. Can you please help? The audio won’t stop playing.

  12. I CANT SEE SHIT!!!!

  13. Yeah, like many others here, I cannot get the sounds to stop playing. Some more annotation about how to implement that is sort of critical. Just spent hours trying to get it to stop, and no luck. Will stop in the local player, but when deployed, on the server, audio will not stop.

  14. Use loader.unloadAndStop(); to stop player and audio

  15. Looks like player.setSize(480, 360); only work in server, not local

  16. How do I remove player controls?

    And how do I autoplay?

    Thanx for a great script 🙂

  1. 1 Trackback(s)

  2. Jak vložit video z YouTube do Flashe (AS3) | Hondzyk blog

Post a Comment