YouTube AS3 Example Using Flash Professional

Based on comments received in my previous article I’ve decided to also produce an example using Flash Professional CS4. The approach is different in some ways, but very similar in others.

View the Example:

This movie requires Flash Player 9

References:
The ActionScript 3 YouTube Chromeless Player is Now Live
YouTube ActionScript 3.0 Player API Reference

Download the Example:
YouTubeAS3_CS4.zip

View Code for Flash Professional:

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
package {
	import flash.system.Security;
	import flash.display.MovieClip;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.net.URLRequest;
	import fl.data.DataProvider;
	import fl.controls.ComboBox;
	import fl.controls.TextArea;
 
	public class YouTubeAS3 extends MovieClip {
		public var VidHolder:MovieClip;
		public var VidSelection:ComboBox;
		public var traceArea:TextArea;
 
		private var player:Object;
		private var loader:Loader;
		private var vidCollection:DataProvider;
 
		public function YouTubeAS3():void {
			Security.allowInsecureDomain("*");
			Security.allowDomain("*");
 
			vidCollection = new DataProvider();
			vidCollection.addItem({data:"KhAplw0Z8zQ", label:"Wreckage"});
			vidCollection.addItem({data:"d54AA2YWll0", label:"Window View"});
			vidCollection.addItem({data:"Sv83GeuyN8A", label:"The Fearless Man"});
			vidCollection.addItem({data:"9t5guYGbuZs", label:"Ephemeral"});
 
			VidSelection.dataProvider = vidCollection;
			VidSelection.addEventListener(Event.CHANGE, cueVideo);
 
			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 {
			VidHolder.addChild(loader);
			loader.content.addEventListener("onReady", onPlayerReady);
			loader.content.addEventListener("onError", onPlayerError);
			loader.content.addEventListener("onStateChange", onPlayerStateChange);
			loader.content.addEventListener("onPlaybackQualityChange", onVideoPlaybackQualityChange);
		}
 
		private function onPlayerReady(event:Event):void {
			traceArea.text += "player ready:" + Object(event).data + "\r";
			player = loader.content;
			player.setSize(VidHolder.width, VidHolder.height);
			VidSelection.selectedIndex = 0;
			VidSelection.dispatchEvent(new Event(Event.CHANGE));
		}
		private function cueVideo(event:Event):void {
			traceArea.text += "switch to:" + event.target.selectedItem.label + "\r";
			player.cueVideoById(event.target.selectedItem.data);
		}
 
		private function onPlayerError(event:Event):void {
			traceArea.text += "player error:" + Object(event).data + "\r";
		}
 
		private function onPlayerStateChange(event:Event):void {
			traceArea.text += "player state:" + Object(event).data + "\r";
		}
 
		private function onVideoPlaybackQualityChange(event:Event):void {
			traceArea.text += "video quality:" + Object(event).data + "\r";
		}
	}
}
This entry was posted in ActionScript and tagged , , , . Bookmark the permalink.

10 Responses to YouTube AS3 Example Using Flash Professional

  1. Pingback: Twitted by kevglx

  2. Pingback: Squidoo Lens Tutorial ~ Reviews and a Video | Beginner’s Internet … « Wilson's Blog

  3. Pingback: The new YouTube AS3 Chromeless API and Flex, Air, Flash | der hess

  4. Florian says:

    Hi Jospeh,

    thanks for your tutorial. Sometimes it is a little bit ugly to work with the You Tube Chromeless API. I like to have some type-safed objects and code-completion. That was the reason why I created some easy to use classes for Flash, Flex and Air.
    http://blog.derhess.de/2009/10/27/the-new-youtube-as3-chromeless-api-in-flex-air-flash/

    Maybe it is interesting for you and your readers?!

    Best regards,
    Flo

  5. dave says:

    Thanks for this example, been looking for something so simple and all the others I have found were going into the api. Nice. Thanks.

  6. mike says:

    I can’t seem to get this to work. keep getting the error Load Never Completed. ????

  7. Nick says:

    Hi, Joseph.
    I can’t make it work even right here in your blog. Its trace field writes player error: 0. Both in FF and IE.

    And could you upload a CS3 example? I cant open it in my CS3 flash.

  8. Steven Donea says:

    I can’t make thise code work in FlashCS3.
    Type was not found on these:
    public var VidSelection:ComboBox;
    public var traceArea:TextArea;
    private var vidCollection:DataProvider;

    and Definition could not be found on:
    import fl.data.DataProvider;
    import fl.controls.ComboBox;
    import fl.controls.TextArea;

    Would be nice if somebody can help. thanks, Steven

Leave a Reply

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

*

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> <pre lang="" line="" escaped="">