source: trunk/as3/com/jeroenwijering/player/SWFLoader.as @ 57

Revision 57, 3.2 KB checked in by jeroen, 5 years ago (diff)

added 3.17 rotator and :void returns to as3 functions

Line 
1/**
2* Loads external SWF skins and plugins.
3**/
4
5
6package com.jeroenwijering.player {
7
8
9import com.jeroenwijering.utils.Draw;
10import flash.display.Loader;
11import flash.display.MovieClip;
12import flash.events.*;
13import flash.net.URLRequest;
14import flash.system.*;
15
16
17public class SWFLoader extends EventDispatcher {
18
19
20        /** Reference to the player itself. **/
21        private var player:MovieClip;
22        /** Reference to the stage graphics. **/
23        public var skin:MovieClip;
24        /** SWF loader reference **/
25        private var loader:Loader;
26        /** Base directory for the plugins. **/
27        private var basedir:String = 'http://plugins.longtailvideo.com/';
28        /** Amount of plugins still to load. **/
29        private var amount:Number;
30
31
32        /**
33        * Constructor.
34        *
35        * @param ply    The player instance.
36        **/
37        public function SWFLoader(ply:MovieClip):void {
38                player = ply;
39                amount = 0;
40        };
41
42
43        /**
44        * Load a list of SWF plugins.
45        *
46        * @prm pgi              A commaseparated list with plugins.
47        **/
48        public function loadPlugins(pgi:String=null):void {
49                if(pgi) {
50                        var arr = pgi.split(',');
51                        amount = arr.length;
52                        for(var i in arr) { loadSWF(arr[i],false); }
53                } else {
54                        dispatchEvent(new Event(Event.COMPLETE));
55                }
56        };
57
58
59        /**
60        * Start the loading process.
61        *
62        * @param cfg    Object that contains all configuration parameters.
63        **/
64        public function loadSkin(skn:String=null):void {
65                if(skn) {
66                        loadSWF(skn,true);
67                } else {
68                        skin = player['player'];
69                        dispatchEvent(new Event(Event.INIT));
70                }
71        };
72
73
74        /** Load a particular SWF file. **/
75        public function loadSWF(str:String,skn:Boolean):void {
76                if(str.substr(-4) != '.swf') { str += '.swf'; }
77                var ldr = new Loader();
78                if(skn) {
79                        ldr.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,skinError);
80                        ldr.contentLoaderInfo.addEventListener(Event.INIT,skinHandler);
81                } else {
82                        skin.addChild(ldr);
83                        ldr.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,pluginError);
84                        ldr.contentLoaderInfo.addEventListener(Event.INIT,pluginHandler);
85                }
86                if(player.loaderInfo.url.indexOf('http://') == 0) {
87                        var ctx = new LoaderContext(true,ApplicationDomain.currentDomain,SecurityDomain.currentDomain);
88                        if(skn) {
89                                ldr.load(new URLRequest(str),ctx);
90                        } else {
91                                ldr.load(new URLRequest(basedir+str),ctx);
92                        }
93                } else {
94                        ldr.load(new URLRequest(str));
95                }
96        };
97
98
99        /** SWF loading failed; use default skin. **/
100        private function pluginError(evt:IOErrorEvent):void {
101                amount--;
102                if (amount == 0) {
103                        dispatchEvent(new Event(Event.COMPLETE));
104                }
105        };
106
107
108        /** Plugin loading completed; add to stage and populate. **/
109        private function pluginHandler(evt:Event):void {
110                var clp = evt.target.content;
111                player.addPlugin(clp);
112                amount--;
113                if (amount == 0) {
114                        dispatchEvent(new Event(Event.COMPLETE));
115                }
116        };
117
118
119        /** SWF loading failed; use default skin. **/
120        private function skinError(evt:IOErrorEvent=null):void {
121                skin = player['player'];
122                dispatchEvent(new Event(Event.INIT));
123        };
124
125
126        /** Skin loading completed; add to stage and populate. **/
127        private function skinHandler(evt:Event):void {
128                var clp = evt.target.content;
129                if(clp['player']) {
130                        skin = MovieClip(clp['player']);
131                        Draw.clear(player);
132                        player.addChild(skin);
133                        dispatchEvent(new Event(Event.INIT));
134                } else {
135                        skinError();
136                }
137        };
138
139}
140
141
142}
Note: See TracBrowser for help on using the repository browser.