| 1 | /** |
|---|
| 2 | * Loads external SWF skins and plugins. |
|---|
| 3 | **/ |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | package com.jeroenwijering.player { |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | import com.jeroenwijering.utils.Draw; |
|---|
| 10 | import flash.display.Loader; |
|---|
| 11 | import flash.display.MovieClip; |
|---|
| 12 | import flash.events.*; |
|---|
| 13 | import flash.net.URLRequest; |
|---|
| 14 | import flash.system.*; |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | public 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 | **/ |
|---|
| 35 | public function SWFLoader(ply:MovieClip):void { |
|---|
| 36 | player = ply; |
|---|
| 37 | }; |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | /** |
|---|
| 41 | * Load a list of SWF plugins. |
|---|
| 42 | * |
|---|
| 43 | * @prm pgi A commaseparated list with plugins. |
|---|
| 44 | **/ |
|---|
| 45 | public function loadPlugins(pgi:String=null):void { |
|---|
| 46 | if(pgi) { |
|---|
| 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 | **/ |
|---|
| 58 | public function loadSkin(skn:String=null):void { |
|---|
| 59 | if(skn) { |
|---|
| 60 | loadSWF(skn,true); |
|---|
| 61 | } else { |
|---|
| 62 | skin = player['player']; |
|---|
| 63 | dispatchEvent(new Event(Event.COMPLETE)); |
|---|
| 64 | } |
|---|
| 65 | }; |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | /** Load a particular SWF file. **/ |
|---|
| 69 | public function loadSWF(str:String,skn:Boolean):void { |
|---|
| 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); |
|---|
| 75 | } else { |
|---|
| 76 | skin.addChild(ldr); |
|---|
| 77 | ldr.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,pluginError); |
|---|
| 78 | ldr.contentLoaderInfo.addEventListener(Event.INIT,pluginHandler); |
|---|
| 79 | } |
|---|
| 80 | if(player.loaderInfo.url.indexOf('http') == 0) { |
|---|
| 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 | |
|---|
| 93 | /** SWF loading failed. **/ |
|---|
| 94 | private function pluginError(evt:IOErrorEvent):void { |
|---|
| 95 | player.view.sendEvent('trace',' plugin: '+evt.toString()); |
|---|
| 96 | }; |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | /** Plugin loading completed; add to stage and populate. **/ |
|---|
| 100 | private function pluginHandler(evt:Event):void { |
|---|
| 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); |
|---|
| 106 | } |
|---|
| 107 | }; |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | /** SWF loading failed; use default skin. **/ |
|---|
| 111 | private function skinError(evt:IOErrorEvent=null):void { |
|---|
| 112 | skin = player['player']; |
|---|
| 113 | dispatchEvent(new Event(Event.COMPLETE)); |
|---|
| 114 | }; |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | /** Skin loading completed; add to stage and populate. **/ |
|---|
| 118 | private function skinHandler(evt:Event):void { |
|---|
| 119 | var clp = evt.target.content; |
|---|
| 120 | if(clp['player']) { |
|---|
| 121 | skin = MovieClip(clp['player']); |
|---|
| 122 | Draw.clear(player); |
|---|
| 123 | player.addChild(skin); |
|---|
| 124 | dispatchEvent(new Event(Event.COMPLETE)); |
|---|
| 125 | } else { |
|---|
| 126 | skinError(); |
|---|
| 127 | } |
|---|
| 128 | }; |
|---|
| 129 | |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | |
|---|
| 133 | } |
|---|