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

Revision 60, 3.0 KB checked in by jeroen, 5 years ago (diff)

changed SWF loading a little, added plugiins to trunk

RevLine 
[49]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
29
30        /**
31        * Constructor.
32        *
33        * @param ply    The player instance.
34        **/
[57]35        public function SWFLoader(ply:MovieClip):void {
[49]36                player = ply;
37        };
38
39
40        /**
41        * Load a list of SWF plugins.
42        *
[60]43        * @prm pgi      A commaseparated list with plugins.
[49]44        **/
[57]45        public function loadPlugins(pgi:String=null):void {
[52]46                if(pgi) {
[49]47                        var arr = pgi.split(',');
48                        for(var i in arr) { loadSWF(arr[i],false); }
49                }
50        };
51
52
53        /**
54        * Start the loading process.
55        *
56        * @param cfg    Object that contains all configuration parameters.
57        **/
[57]58        public function loadSkin(skn:String=null):void {
[49]59                if(skn) {
60                        loadSWF(skn,true);
61                } else {
62                        skin = player['player'];
[60]63                        dispatchEvent(new Event(Event.COMPLETE));
[49]64                }
65        };
66
67
68        /** Load a particular SWF file. **/
[57]69        public function loadSWF(str:String,skn:Boolean):void {
[49]70                if(str.substr(-4) != '.swf') { str += '.swf'; }
71                var ldr = new Loader();
72                if(skn) {
73                        ldr.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,skinError);
74                        ldr.contentLoaderInfo.addEventListener(Event.INIT,skinHandler);
[60]75                } else {
[49]76                        skin.addChild(ldr);
77                        ldr.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,pluginError);
78                        ldr.contentLoaderInfo.addEventListener(Event.INIT,pluginHandler);
79                }
[60]80                if(player.loaderInfo.url.indexOf('http') == 0) {
[49]81                        var ctx = new LoaderContext(true,ApplicationDomain.currentDomain,SecurityDomain.currentDomain);
82                        if(skn) {
83                                ldr.load(new URLRequest(str),ctx);
84                        } else {
85                                ldr.load(new URLRequest(basedir+str),ctx);
86                        }
87                } else {
88                        ldr.load(new URLRequest(str));
89                }
90        };
91
92
[60]93        /** SWF loading failed. **/
[57]94        private function pluginError(evt:IOErrorEvent):void {
[60]95                player.view.sendEvent('trace',' plugin: '+evt.toString());
[49]96        };
97
98
99        /** Plugin loading completed; add to stage and populate. **/
[57]100        private function pluginHandler(evt:Event):void {
[60]101                var plg = evt.target.content;
102                try {
103                        plg.initializePlugin(player.view);
104                } catch(err:Error) {
105                        player.view.sendEvent('trace',' plugin: '+err.message);
[49]106                }
107        };
108
109
110        /** SWF loading failed; use default skin. **/
[57]111        private function skinError(evt:IOErrorEvent=null):void {
[49]112                skin = player['player'];
[60]113                dispatchEvent(new Event(Event.COMPLETE));
[49]114        };
115
116
117        /** Skin loading completed; add to stage and populate. **/
[57]118        private function skinHandler(evt:Event):void {
[49]119                var clp = evt.target.content;
120                if(clp['player']) {
121                        skin = MovieClip(clp['player']);
122                        Draw.clear(player);
123                        player.addChild(skin);
[60]124                        dispatchEvent(new Event(Event.COMPLETE));
[49]125                } else {
126                        skinError();
127                }
128        };
129
130}
131
132
133}
Note: See TracBrowser for help on using the repository browser.