source: trunk/fl5/src/com/longtailvideo/jwplayer/model/Playlist.as @ 729

Revision 729, 5.6 KB checked in by zach, 4 years ago (diff)
Line 
1package com.longtailvideo.jwplayer.model {
2        import com.longtailvideo.jwplayer.events.GlobalEventDispatcher;
3        import com.longtailvideo.jwplayer.events.PlayerEvent;
4        import com.longtailvideo.jwplayer.events.PlaylistEvent;
5        import com.longtailvideo.jwplayer.parsers.IPlaylistParser;
6        import com.longtailvideo.jwplayer.parsers.JWParser;
7        import com.longtailvideo.jwplayer.parsers.ParserFactory;
8        import com.longtailvideo.jwplayer.utils.AssetLoader;
9       
10        import flash.events.ErrorEvent;
11        import flash.events.Event;
12       
13       
14        /**
15         * Sent when a playlist has been loaded.
16         *
17         * @eventType com.longtailvideo.jwplayer.events.PlaylistEvent.JWPLAYER_PLAYLIST_LOADED
18         */
19        [Event(name="jwplayerPlaylistLoaded", type="com.longtailvideo.jwplayer.events.PlaylistEvent")]
20       
21       
22        /**
23         * Sent when the playlist has been updated.
24         *
25         * @eventType com.longtailvideo.jwplayer.events.PlaylistEvent.JWPLAYER_PLAYLIST_UPDATED
26         */
27        [Event(name="jwplayerPlaylistUpdated", type="com.longtailvideo.jwplayer.events.PlaylistEvent")]
28       
29       
30        /**
31         * Sent when the playlist's current item has changed.
32         *
33         * @eventType com.longtailvideo.jwplayer.events.PlaylistEvent.JWPLAYER_PLAYLIST_ITEM
34         */
35        [Event(name="jwplayerPlaylistItem", type="com.longtailvideo.jwplayer.events.PlaylistEvent")]
36       
37       
38        /**
39         * Sent when an error ocurred when loading or parsing the playlist
40         *
41         * @eventType com.longtailvideo.jwplayer.events.PlayerEvent.JWPLAYER_ERROR
42         */
43        [Event(name="jwplayerError", type = "com.longtailvideo.jwplayer.events.PlayerEvent")]
44       
45       
46        public class Playlist extends GlobalEventDispatcher implements IPlaylist {
47                /** **/
48                private var list:Array;
49                /** **/
50                private var index:Number;
51               
52               
53                /**
54                 * Constructor
55                 */
56                public function Playlist() {
57                        list = [];
58                        index = -1;
59                }
60               
61               
62                /**
63                 * @inheritDoc
64                 */
65                public function load(newPlaylist:Object):void {
66                        var newList:Array = [];
67                        if (newPlaylist is Array) {
68                                for (var i:Number = 0; i < (newPlaylist as Array).length; i++) {
69                                        if (!(newPlaylist[i] is PlaylistItem)) {
70                                                var newItem:PlaylistItem = new PlaylistItem(newPlaylist[i]);
71                                                newPlaylist[i] = newItem;
72                                        }
73                                        try {
74                                                if ((newPlaylist[i] as PlaylistItem).file) {
75                                                        newList.push(newPlaylist[i] as PlaylistItem);
76                                                }
77                                        } catch (e:Error) {
78                                        }
79                                }
80                        } else if (newPlaylist is PlaylistItem) {
81                                var pli:PlaylistItem = newPlaylist as PlaylistItem;
82                                JWParser.updateProvider(pli);
83                                if (pli.provider) {
84                                        newList.push(pli);
85                                } else {
86                                        load(pli.file);
87                                        return;
88                                }
89                        } else if (newPlaylist is Playlist) {
90                                for (i = 0; i < (newPlaylist as Playlist).length; i++) {
91                                        newList.push((newPlaylist as Playlist).getItemAt(i));
92                                }
93                        } else if (newPlaylist is String && newPlaylist != "") {
94                                var playlistLoader:AssetLoader = new AssetLoader();
95                                playlistLoader.addEventListener(Event.COMPLETE, playlistLoaded);
96                                playlistLoader.addEventListener(ErrorEvent.ERROR, playlistLoadError);
97                                playlistLoader.load(String(newPlaylist), XML);
98                                return;
99                        } else {
100                                playlistError("Incorrect playlist type");
101                                return;
102                        }
103                        if (newList.length > 0) {
104                                list = newList;
105                                index = 0;
106                                dispatchEvent(new PlaylistEvent(PlaylistEvent.JWPLAYER_PLAYLIST_LOADED, this));
107                        } else {
108                                dispatchEvent(new PlayerEvent(PlayerEvent.JWPLAYER_ERROR, "Loaded playlist is empty"));
109                        }
110                        return;
111                }
112               
113               
114                protected function playlistLoaded(evt:Event):void {
115                        var loader:AssetLoader = evt.target as AssetLoader;
116                        var loadedXML:XML = loader.loadedObject as XML;
117                        var parser:IPlaylistParser = ParserFactory.getParser(loadedXML);
118                        var playlistItems:Array = parser.parse(loadedXML);
119                        if (playlistItems.length > 0) {
120                                load(playlistItems);
121                        } else {
122                                playlistError("XML could not be parsed or playlist was empty");
123                        }
124                }
125               
126               
127                protected function playlistLoadError(evt:ErrorEvent):void {
128                        playlistError(evt.text);
129                }
130               
131               
132                protected function playlistError(message:String):void {
133                        dispatchEvent(new PlayerEvent(PlayerEvent.JWPLAYER_ERROR, "Playlist could not be loaded: " + message));
134                }
135               
136               
137                /**
138                 * @inheritDoc
139                 */
140                public function getItemAt(idx:Number):PlaylistItem {
141                        try {
142                                return list[idx];
143                        } catch (e:Error) {
144                        }
145                        return null;
146                }
147               
148               
149                /**
150                 * @inheritDoc
151                 */
152                public function insertItem(itm:PlaylistItem, idx:Number = -1):void {
153                        if (idx >= 0 && idx < list.length) {
154                                list.splice(idx, 0, itm);
155                        } else {
156                                list.push(itm);
157                        }
158                        dispatchEvent(new PlaylistEvent(PlaylistEvent.JWPLAYER_PLAYLIST_UPDATED, this));
159                        if (index < 0) {
160                                currentIndex = list.length - 1;
161                        }
162                }
163               
164               
165                /**
166                 * @inheritDoc
167                 */
168                public function removeItemAt(idx:Number):void {
169                        if (idx >= 0 && idx < list.length && list.length > 0) {
170                                list.splice(idx, 1);
171                                dispatchEvent(new PlaylistEvent(PlaylistEvent.JWPLAYER_PLAYLIST_UPDATED, this));
172                        }
173                        if (index >= list.length) {
174                                currentIndex = list.length - 1;
175                        }
176                }
177               
178               
179                /**
180                 * @inheritDoc
181                 */
182                public function get currentIndex():Number {
183                        return index;
184                }
185               
186               
187                /**
188                 * @inheritDoc
189                 */
190                public function set currentIndex(idx:Number):void {
191                        if (idx != index && idx < list.length) {
192                                if (idx >= 0) {
193                                        index = idx;
194                                        dispatchEvent(new PlaylistEvent(PlaylistEvent.JWPLAYER_PLAYLIST_ITEM, this));
195                                } else {
196                                        index = -1;
197                                }
198                        }
199                }
200               
201               
202                /**
203                 * @inheritDoc
204                 */
205                public function get currentItem():PlaylistItem {
206                        return index >= 0 ? getItemAt(index) : null;
207                }
208               
209                /**
210                 * @inheritDoc
211                 */
212                public function get length():Number {
213                        return list.length;
214                }
215
216                /**
217                 * @inheritDoc
218                 **/
219                public function contains(item:PlaylistItem):Boolean {
220                        for (var i:Number=0; i < length; i++) {
221                                if (getItemAt(i) == item) return true;
222                        }
223                        return false;
224                }
225        }
226}
Note: See TracBrowser for help on using the repository browser.