source: trunk/fl5/src/com/longtailvideo/jwplayer/utils/AssetLoader.as @ 770

Revision 770, 3.6 KB checked in by zach, 3 years ago (diff)
Line 
1package com.longtailvideo.jwplayer.utils {
2        import flash.display.Loader;
3        import flash.display.LoaderInfo;
4        import flash.events.ErrorEvent;
5        import flash.events.Event;
6        import flash.events.EventDispatcher;
7        import flash.events.IOErrorEvent;
8        import flash.events.SecurityErrorEvent;
9        import flash.net.URLLoader;
10        import flash.net.URLRequest;
11        import flash.system.ApplicationDomain;
12        import flash.system.LoaderContext;
13        import flash.system.SecurityDomain;
14        import flash.utils.ByteArray;
15
16
17        /**
18         * Sent when the loader has completed loading.  AssetLoader's <code>loadedObject</code> now contains the loaded content.
19         *
20         * @eventType flash.events.Event.COMPLETE
21         */
22        [Event(name="complete", type="flash.events.Event")]
23
24        /**
25         * Sent when an error occurred loading or casting the content
26         *
27         * @eventType flash.events.ErrorEvent.ERROR
28         */
29        [Event(name="error", type="flash.events.ErrorEvent")]
30
31        public class AssetLoader extends EventDispatcher {
32                private var _loaderExtensions:Array = ["swf", "png", "gif", "jpg", "jpeg"];
33                private var _loader:Loader;
34                private var _urlLoader:URLLoader;
35                private var LoadedClass:Class;
36                public var loadedObject:*;
37
38
39                public function load(location:String, expectedClass:Class=null):void {
40                        LoadedClass = expectedClass;
41
42                        var ext:String = Strings.extension(location);
43
44                        if (_loaderExtensions.indexOf(ext.toLowerCase()) >= 0) {
45                                useLoader(location);
46                        } else {
47                                useURLLoader(location);
48                        }
49                }
50               
51               
52                public function loadBytes(byteArray:ByteArray):void {
53                        loader.loadBytes(byteArray);
54                }
55
56
57                protected function useLoader(location:String):void {
58                        if (RootReference.root.loaderInfo.url.indexOf('http') == 0) {
59                                var context:LoaderContext = new LoaderContext(true, ApplicationDomain.currentDomain, SecurityDomain.currentDomain);
60                                loader.load(new URLRequest(location), context);
61                        } else {
62                                loader.load(new URLRequest(location));
63                        }
64                }
65
66
67                protected function loadComplete(evt:Event):void {
68                        try {
69                                if (LoadedClass) {
70                                        loadedObject = (evt.target as LoaderInfo).content as LoadedClass;
71                                } else {
72                                        loadedObject = (evt.target as LoaderInfo).content;
73                                }
74                                dispatchEvent(new Event(Event.COMPLETE));
75                        } catch (e:Error) {
76                                dispatchEvent(new ErrorEvent(ErrorEvent.ERROR, false, false, e.message));
77                        }
78                }
79
80
81                protected function loadError(evt:ErrorEvent):void {
82                        dispatchEvent(new ErrorEvent(ErrorEvent.ERROR, false, false, evt.text));
83                }
84
85
86                protected function get loader():Loader {
87                        if (!_loader) {
88                                _loader = new Loader();
89                                _loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete);
90                                _loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, loadError);
91                                _loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR, loadError);
92                        }
93                        return _loader;
94                }
95
96
97                protected function useURLLoader(location:String):void {
98                        urlLoader.load(new URLRequest(location));
99                }
100
101
102                protected function urlLoadComplete(evt:Event):void {
103                        try {
104                                if (LoadedClass) {
105                                        loadedObject = LoadedClass((evt.target as URLLoader).data);
106                                } else {
107                                        loadedObject = (evt.target as URLLoader).data;
108                                }
109                                dispatchEvent(new Event(Event.COMPLETE));
110                        } catch (e:Error) {
111                                dispatchEvent(new ErrorEvent(ErrorEvent.ERROR, false, false, e.message));
112                        }
113                }
114
115
116                protected function get urlLoader():URLLoader {
117                        if (!_urlLoader) {
118                                _urlLoader = new URLLoader();
119                                _urlLoader.addEventListener(Event.COMPLETE, urlLoadComplete);
120                                _urlLoader.addEventListener(IOErrorEvent.IO_ERROR, loadError);
121                                _urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, loadError);
122                        }
123                        return _urlLoader;
124                }
125        }
126}
Note: See TracBrowser for help on using the repository browser.