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:

Flash Player 11.2 or greater is required!

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";
		}
	}
}

13 comments

  1. Pingback: Twitted by kevglx
  2. Thanks for this example, been looking for something so simple and all the others I have found were going into the api. Nice. Thanks.

  3. 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.

  4. 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

  5. Hi,
    I am using a similar script but without the dropdown menu.
    I am getting ‘Security Sandbox Violation’ errors, I am using the following code, any help would be much apreciated?

    // start
    Security.allowDomain(“www.youtube.com”);
    var my_player:Object;

    var my_loader:Loader = new Loader();
    my_loader.load(new URLRequest(“http://www.youtube.com/v/CtOj8kpLIlI?version=3”));
    my_loader.contentLoaderInfo.addEventListener(Event.INIT, onLoaderInit);

    function onLoaderInit(e:Event):void{
    movArticles.movArticle2.videoPlayerHolder.videoMov2.addChild(my_loader);
    my_player = my_loader.content;
    my_player.addEventListener(“onReady”, onPlayerReady);
    }

    function onPlayerReady(e:Event):void{
    my_player.setSize(640,360);
    my_player.cueVideoById(“CtOj8kpLIlI”,0);
    }
    // end

Comments are closed.