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

Revision 1149, 6.4 KB checked in by pablo, 3 years ago (diff)

A few minor bug fixes and tweaks:

  • RTMP mp3 streams w/ query params had the query string truncated instead of the file exension
  • More descriptive error message for playlist error 2048 (crossdomain security restrictions)
  • Loading error screen displays error message
  • Removed unnecessary AS2 TEA class
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                /** An array holding all of the PlaylistItem objects **/
48                private var list:Array;
49                /** The current playlist index **/
50                private var index:Number;
51                /** Keep track of the last playlistItem, so we can send a PLAYLIST_ITEM event at the correct time **/
52                private var lastItem:PlaylistItem = null;
53                /** AssetLoader to grab playlist XML files **/
54                private var playlistLoader:AssetLoader;
55               
56                /**
57                 * Constructor
58                 */
59                public function Playlist() {
60                        list = [];
61                        index = -1;
62                        playlistLoader = new AssetLoader();
63                        playlistLoader.addEventListener(Event.COMPLETE, playlistLoaded);
64                        playlistLoader.addEventListener(ErrorEvent.ERROR, playlistLoadError);
65                }
66               
67               
68                /**
69                 * @inheritDoc
70                 */
71                public function load(newPlaylist:Object):void {
72                        var newList:Array = [];
73                        if (newPlaylist is Array) {
74                                for (var i:Number = 0; i < (newPlaylist as Array).length; i++) {
75                                        if (!(newPlaylist[i] is PlaylistItem)) {
76                                                var newItem:PlaylistItem = new PlaylistItem(newPlaylist[i]);
77                                                newPlaylist[i] = newItem;
78                                        }
79                                        try {
80                                                if ((newPlaylist[i] as PlaylistItem).file) {
81                                                        newList.push(newPlaylist[i] as PlaylistItem);
82                                                }
83                                        } catch (e:Error) {
84                                        }
85                                }
86                        } else if (newPlaylist is PlaylistItem) {
87                                var pli:PlaylistItem = newPlaylist as PlaylistItem;
88                                if (JWParser.getProvider(pli)) {
89                                        newList.push(pli);
90                                } else {
91                                        load(pli.file);
92                                        return;
93                                }
94                        } else if (newPlaylist is Playlist) {
95                                for (i = 0; i < (newPlaylist as Playlist).length; i++) {
96                                        newList.push((newPlaylist as Playlist).getItemAt(i));
97                                }
98                        } else if (newPlaylist is String && newPlaylist != "") {
99                                playlistLoader.load(String(newPlaylist), XML);
100                                return;
101                        } else {
102                                playlistError("Incorrect playlist type");
103                                return;
104                        }
105                        if (newList.length > 0) {
106                                for each(var item:PlaylistItem in newList) {
107                                        if (!item.provider) {
108                                                item.provider = JWParser.getProvider(item);
109                                        }
110                                }
111                                list = newList;
112                                index = 0;
113                                dispatchEvent(new PlaylistEvent(PlaylistEvent.JWPLAYER_PLAYLIST_LOADED, this));
114                        } else {
115                                dispatchEvent(new PlayerEvent(PlayerEvent.JWPLAYER_ERROR, "Loaded playlist is empty"));
116                        }
117                        return;
118                }
119               
120               
121                protected function playlistLoaded(evt:Event):void {
122                        var loadedXML:XML = playlistLoader.loadedObject as XML;
123                        var parser:IPlaylistParser = ParserFactory.getParser(loadedXML);
124                        if (parser) {
125                                var playlistItems:Array = parser.parse(loadedXML);
126                                if (playlistItems.length > 0) {
127                                        load(playlistItems);
128                                } else {
129                                        playlistError("XML could not be parsed or playlist was empty");
130                                }
131                        } else {
132                                playlistError("Playlist file did not contain a valid playlist");
133                        }
134                }
135               
136               
137                protected function playlistLoadError(evt:ErrorEvent):void {
138                        playlistError(evt.text);
139                }
140               
141               
142                protected function playlistError(message:String):void {
143                        if (message.indexOf("Error #2048") >= 0) {
144                                dispatchEvent(new PlayerEvent(PlayerEvent.JWPLAYER_ERROR, "Playlist could not be loaded due to crossdomain policy restrictions."));
145                        } else {
146                                dispatchEvent(new PlayerEvent(PlayerEvent.JWPLAYER_ERROR, "Playlist could not be loaded: " + message));
147                        }
148                }
149               
150               
151                /**
152                 * @inheritDoc
153                 */
154                public function getItemAt(idx:Number):PlaylistItem {
155                        try {
156                                return list[idx];
157                        } catch (e:Error) {
158                        }
159                        return null;
160                }
161               
162               
163                /**
164                 * @inheritDoc
165                 */
166                public function insertItem(itm:PlaylistItem, idx:Number = -1):void {
167                        if (idx >= 0 && idx < list.length) {
168                                list.splice(idx, 0, itm);
169                        } else {
170                                list.push(itm);
171                        }
172                        dispatchEvent(new PlaylistEvent(PlaylistEvent.JWPLAYER_PLAYLIST_UPDATED, this));
173                        if (index < 0) {
174                                currentIndex = list.length - 1;
175                        }
176                }
177               
178               
179                /**
180                 * @inheritDoc
181                 */
182                public function removeItemAt(idx:Number):void {
183                        if (idx >= 0 && idx < list.length && list.length > 0) {
184                                list.splice(idx, 1);
185                                dispatchEvent(new PlaylistEvent(PlaylistEvent.JWPLAYER_PLAYLIST_UPDATED, this));
186                        }
187                        if (index >= list.length) {
188                                currentIndex = list.length - 1;
189                        }
190                }
191               
192               
193                /**
194                 * @inheritDoc
195                 */
196                public function get currentIndex():Number {
197                        return index;
198                }
199               
200               
201                /**
202                 * @inheritDoc
203                 */
204                public function set currentIndex(idx:Number):void {
205                        if (idx > list.length) idx = 0;
206                        if (idx >= 0) {
207                                index = idx;
208                                if (getItemAt(idx) != lastItem) {
209                                        lastItem = currentItem;
210                                        dispatchEvent(new PlaylistEvent(PlaylistEvent.JWPLAYER_PLAYLIST_ITEM, this));
211                                }
212                        } else {
213                                lastItem = null;
214                                index = -1;
215                        }
216                }
217               
218               
219                /**
220                 * @inheritDoc
221                 */
222                public function get currentItem():PlaylistItem {
223                        return index >= 0 ? getItemAt(index) : null;
224                }
225               
226                /**
227                 * @inheritDoc
228                 */
229                public function get length():Number {
230                        return list.length;
231                }
232               
233                /**
234                 * @inheritDoc
235                 **/
236                public function contains(item:PlaylistItem):Boolean {
237                        for (var i:Number=0; i < length; i++) {
238                                if (getItemAt(i) == item) return true;
239                        }
240                        return false;
241                }
242        }
243}
Note: See TracBrowser for help on using the repository browser.