Sunday, 23 May 2010

YouTube videos in Actionscript 3

In this post I will create a Youtube player in Actionscript 3 using the YouTube API. The API lets you play YT videos directly inside of your Flash application without needing to navigate to the YT website. There are two types of players: Embedded player and Chromeless player. The Embedded player has video playback controls such as play, pause, seek etc and is similar in appearance to the player’s in the YT website. Whereas the Chromeless player has no video play back controls. I will be creating a Chromeless player and adding my own custom controls. The documentation for the API can be found here.

Security.allowDomain("www.youtube.com");

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(e:Event):void {
    addChild(loader);
    loader.content.addEventListener("onReady", onPlayerReady);
    player = loader.content;
}

function onPlayerReady(e:Event):void {
    player.setSize(480, 360);
    playBtn.addEventListener(MouseEvent.CLICK, playHandler);
    
}

function playHandler(e:MouseEvent):void {
    playBtn.visible = false;
    player.cueVideoByUrl("FzRH3iTQPrk", 0, "medium");
}

The YT API has a number of methods to control the playback the player. Take a look at the documentation for more information.

0 comments:

  COPYRIGHT © 2014 · ILIKE2FLASH · Theme by Ourblogtemplates

Back to TOP