Sample Code – Using the YouTube AS3 API with the YouTube Player API

Hi Everyone,

I have been absent for a while.. taking advantage of life. I also have switched to Android lately, doing some prototyping work with 80/20 Studio in NYC.

Someone from Belgium contacted me today about the YouTube AS3 API, so I decided to look into it once again. While browsing the Youtube API site I found that they have finally published a AS3 Player that can be used!! So here is some sample code on how to do it.

Warning, in the onPlayerError be sure to read what is coming out. Some videos (the first result from this sample search) are not allowed to be played on foreign sites (copyright content mostly).

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
package 
{
	import ca.newcommerce.youtube.reference.SearchOrder;
	import ca.newcommerce.youtube.reference.SearchRacy;
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.net.URLRequest;
	import flash.system.Security;
 
	import ca.newcommerce.youtube.data.ThumbnailData;
	import ca.newcommerce.youtube.data.VideoData;
	import ca.newcommerce.youtube.events.VideoFeedEvent;
	import ca.newcommerce.youtube.feeds.VideoFeed;
	import ca.newcommerce.youtube.iterators.ThumbnailIterator;
	import ca.newcommerce.youtube.webservice.YouTubeFeedClient;	
 
	/**
	 * ...
	 * @author Martin Legris ( http://blog.martinlegris.com )
	 */
	public class Main extends Sprite 
	{
		protected var _ws:YouTubeFeedClient;
		protected var _requestId:Number;
		protected var _player:Object = null;
		protected var _video:VideoData = null;
		protected var _playerLoader:Loader;
 
		public function Main():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
 
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			testSearch();
			loadPlayer();
		}
 
		protected function loadPlayer():void
		{
			trace("loadPlayer");
			_playerLoader = new Loader();
			_playerLoader.contentLoaderInfo.addEventListener(Event.INIT, onLoaderInit);
			_playerLoader.load(new URLRequest("http://www.youtube.com/apiplayer?version=3"));
		}
 
		protected function onLoaderInit(evt:Event):void
		{
			trace("onLoaderInit");
 
			addChild(_playerLoader);
			var c:DisplayObject = _playerLoader.content;
			c.addEventListener("onReady", onPlayerReady);
			c.addEventListener("onError", onPlayerError);
			c.addEventListener("onStateChange", onPlayerStateChange);
			c.addEventListener("onPlaybackQualityChange", onVideoPlaybackQualityChange);
		}
 
		protected function onPlayerReady(evt:Event):void
		{
			trace("onPlayerReady");
 
			_player = _playerLoader.content;
			_player.setSize(480, 360);
			if (_video != null)
				startPlaying();
		}
 
		protected function onPlayerError(evt:Event):void
		{
			trace("onPlayerError:" + Object(evt).data);
		}
 
		protected function onPlayerStateChange(evt:Event):void
		{
			trace("onPlayerStateChange:" + Object(evt).data);
		}
 
		protected function onVideoPlaybackQualityChange(evt:Event):void
		{
			trace("onVideoPlaybackQualityChange:" + Object(evt).data);
		}
 
		protected function testSearch():void
		{
			Security.allowDomain("www.youtube.com");
 
			_ws = YouTubeFeedClient.getInstance();
 
			_ws.addEventListener(VideoFeedEvent.VIDEO_DATA_RECEIVED, doSearchResults);
			_requestId = _ws.getVideos("american idol", "", null, null, ["music"], ["male"], SearchOrder.ORDER_BY_VIEWCOUNT, SearchRacy.RACY_INCLUDE);
 
			_ws.getVideos("music video");
		}
 
		protected function doSearchResults(evt:VideoFeedEvent):void
		{
			if(_requestId == evt.requestId)
			{
				var feed:VideoFeed = evt.feed;
				_video = feed.getAt(2);
			}
			else
			{
				trace("this call:"+evt.requestId+" isn't ours. We'll wait for the next one...");
			}
 
			if (_player != null)
			{
				startPlaying();
			}
		}
 
		protected function startPlaying():void
		{
			var id:String = _video.id;
			id = id.substr(id.lastIndexOf("/") + 1);
			trace("loading video by id:" + id);
			trace("loading video:" + _video.actualId);			
			_player.loadVideoById(_video.actualId);
		}
	}
}

Cheers!

2 thoughts on “Sample Code – Using the YouTube AS3 API with the YouTube Player API

  1. Martin
    I do not want these restricted videos to be made available. How would I
    a) exclude them from the getVideos request
    and/or b) find the yt:state tag in the feed returned

    cheers

  2. Why don’t you implement to get a single video’s data, such as title, description etc.? I think it would be used often.

    Looking forward to the Flex version.

    respects

Leave a Reply

Your email address will not be published. Required fields are marked *

* Copy This Password *

* Type Or Paste Password Here *

56,102 Spam Comments Blocked so far by Spam Free Wordpress

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>