Custom interface for YouTube videos
I would like to break away from games and physics engines for this post to demonstrate something new that I learned last week at work. I had to use the new YouTube AS3 API to import video hosted on YouTube, into the clients site. They didn’t want the standard YouTube player interface, but something a lot simpler and styled to the website.
The API is very easy to use, you don’t even need to import custom scripts into your code. All you need to do is load in the chrome less player from YouTube’s site with Flash’s Loader Class and you will then have access to all the functions and properties of the player. The most important thing to note though, is that since this is AS3, you need to the YouTube domain security clearance in order to be able to communicate between your swf, and the one you load in from YouTube. Let’s take a look at the code.
You can view my custom player here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | package { //The usual imports import flash.display.*; import flash.net.URLRequest; import flash.events.*; //Security to allow communication with the YouTube swf import flash.system.Security; //A great Tween package that I use all the time import com.greensock.TweenLite; public class YouTubeVideo extends flash.display.MovieClip { private var _player:Object; //The player private var _loader:Loader; //The loader to get the player //Three buttons: Pause, Play and Fullscreen private var _fullButton:SimpleButton; private var _pauseButton:SimpleButton; private var _playButton:SimpleButton; private var _controls:Sprite; //A sprite to hold the controls private var _blank:Sprite; //A sprite to act as a detect area for the controls public function YouTubeVideo() { //Allow coms between our swf and YouTube's Security.allowDomain("www.youtube.com"); //Load in the chromeless YouTube player swf _loader = new Loader(); _loader.contentLoaderInfo.addEventListener(Event.INIT, onLoaderInit); _loader.load(new URLRequest("http://www.youtube.com/apiplayer?version=3")); } private function onLoaderInit(event:Event):void { addChild(_loader); //Add the loader to the display list _player = _loader.content; //Load the content of the loader into a player object //Set up a bunch of event listeners on the player to handle any changes _player.addEventListener("onReady", onPlayerReady); _player.addEventListener("onError", onPlayerError); _player.addEventListener("onStateChange", onPlayerStateChange); _player.addEventListener("onPlaybackQualityChange", onVideoPlaybackQualityChange); } private function onPlayerReady(event:Event):void { // Once this event has been dispatched by the player, we can use // cueVideoById, loadVideoById, cueVideoByUrl and loadVideoByUrl // to load a particular YouTube video. _player.loadVideoById("YOUTUBE_ID", 0, "default"); // Set appropriate player dimensions for your application _player.setSize(533, 300); //Add controls to the display list _controls = new Sprite(); addChild(_controls); //This invisible sprite gives us an area of interaction for the controls //which we can use to fade out the controls when the mouse leaves. _blank = new Blank(); _blank.width = 485; _blank.height = 240; _blank.x = 30; _blank.y = 30; _controls.addChild(_blank); //Create the three buttons we want for the interface and add //mouse click event listeners to them _fullButton = new FullScreenButton(); _fullButton.addEventListener(MouseEvent.CLICK, onFullScreenClick); _fullButton.x = 40; _fullButton.y = 240; _controls.addChild(_fullButton); _pauseButton = new PauseButton(); _pauseButton.addEventListener(MouseEvent.CLICK, onPauseClick); _pauseButton.x = 240; _pauseButton.y = 120; _controls.addChild(_pauseButton); //Play button isn't added to the display list as the video is already playing //and so only the pause button can be seen _playButton = new PlayButton(); _playButton.addEventListener(MouseEvent.CLICK, onPlayClick); _playButton.x = 240; _playButton.y = 120; //Events to detect if the mouse is hovering over the controls _controls.addEventListener(MouseEvent.ROLL_OUT, onMouseOut); _controls.addEventListener(MouseEvent.ROLL_OVER, onMouseOver); } private function onMouseOver(e:MouseEvent):void { //When the mouse is over the controls, fade them up using the //custom Tween class we already imported TweenLite.to(_playButton, .6, {alpha:1}); TweenLite.to(_pauseButton, .6, {alpha:1}); TweenLite.to(_fullButton, .6, {alpha:1}); } private function onMouseOut(e:MouseEvent):void { //And fade them down when the mouse leaves TweenLite.to(_playButton, .6, {alpha:0}); TweenLite.to(_pauseButton, .6, {alpha:0}); TweenLite.to(_fullButton, .6, {alpha:0}); } private function onFullScreenClick(e:MouseEvent):void { //This function toggles the fullscreen on and off if (this.stage.displayState == StageDisplayState.FULL_SCREEN) { this.stage.displayState=StageDisplayState.NORMAL; //Actions for going to normal mode from full screen } else { this.stage.displayState=StageDisplayState.FULL_SCREEN; //Actions for going to full screen mode, for example, //masks will need to be turned off in full screen mode. } } private function onPauseClick(e:MouseEvent):void { //When pause is clicked, we pause the video and swap //the pause button for the play button. _player.pauseVideo(); _controls.removeChild(_pauseButton); _controls.addChild(_playButton); } private function onPlayClick(e:MouseEvent):void { //When play is clicked, we play the video and swap //the play button for the pause button. _player.playVideo(); _controls.addChild(_pauseButton); _controls.removeChild(_playButton); } private function onPlayerStateChange(event:Event):void { //Test if the video is finished, and turn the play button on if true if(_player.getPlayerState() == 0) { _controls.removeChild(_pauseButton); _controls.addChild(_playButton); } } } } |
While I am sure you can learn all the details you need from the documentation page for the YouTube AS3 API, I will quickly describe a few of the functions. As you can see, pausing and playing the video is as easy as calling the playVideo() and pauseVideo() functions. You can seek to any point in the timeline using seekTo(seconds:Number, allowSeekAhead:Boolean) and set the volume with setVolume(volume:Number). Starting to get the picture? It is very straight forward and offers a great alternative to hosting your own videos.
Have fun!


