user1070447
user1070447

Reputation: 147

Youtube API functions

This website lists the youtube API functions to communicate with a youtube video. Does anybody know where I could find their implementation? I'm trying to figure out what to pass to SetVariable() to communicate directly with a youtube swf file.

Upvotes: 3

Views: 664

Answers (1)

Swati Singh
Swati Singh

Reputation: 1863

 package 
 {
     import flash.display.DisplayObject;
     import flash.display.Loader;
     import flash.display.Sprite;
     import flash.events.Event;
     import flash.net.URLRequest;

     public class Main extends Sprite 
     {
        private var _loader : Loader;
        private var _player : Object;

        public function Main()
        { 
            _loader = new Loader();
            _loader.contentLoaderInfo.addEventListener(Event.INIT, _onLoaderInit, false, 0, true);
            _loader.load(new URLRequest("http://www.youtube.com/apiplayer?version=3"));
        }

        private function _onLoaderInit(event : Event) : void 
        {
             _player = _loader.content; 
             _player.addEventListener("onReady", _onPlayerReady, false, 0, true);
             addChild(DisplayObject(_player));

            _loader.contentLoaderInfo.removeEventListener(Event.INIT, _onLoaderInit);
            _loader = null;
        }

        private function _onPlayerReady(event : Event) : void 
        {
           _player.removeEventListener("onReady", _onPlayerReady);
           // Once this event has been dispatched by the player, we can use
           // cueVideoById, loadVideoById, cueVideoByUrl and loadVideoByUrl
           // to load a particular YouTube video.  
             _player.setSize(640, 360);
             _player.loadVideoById("D2gqThOfHu4");
        }
     }
  }

if you wanted to use the controls from YouTube you would only have to replace one line:

//_loader.load(new URLRequest("http://www.youtube.com/apiplayer?version=3"));
//replace this line with the following

 _loader.load(new URLRequest("http://www.youtube.com/v/VIDEO_ID?version=3"));
//replace VIDEO_ID with the id of the video you want to load in the previous case :"D2gqThOfHu4"

//also you can comment the following line if you don't want the video to start automatically:
  _player.loadVideoById("D2gqThOfHu4");

Upvotes: 2

Related Questions