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

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