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

Revision 1238, 6.6 KB checked in by pablo, 21 months ago (diff)
  • Maintains aspect ratio going to/from fullscreen if "stretching" is set to "exactfit" 606
  • Automatically adds a thumbnail image for YouTube videos if one is not specified. 642
  • Fades the controlbar when the mouse leaves the screen. Also adds the 'controlbar.idlehide' option to keep the controlbar hidden when not playing. 883
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.media.YouTubeMediaProvider;
6        import com.longtailvideo.jwplayer.parsers.IPlaylistParser;
7        import com.longtailvideo.jwplayer.parsers.JWParser;
8        import com.longtailvideo.jwplayer.parsers.ParserFactory;
9        import com.longtailvideo.jwplayer.utils.AssetLoader;
10       
11        import flash.events.ErrorEvent;
12        import flash.events.Event;
13       
14       
15        /**
16         * Sent when a playlist has been loaded.
17         *
18         * @eventType com.longtailvideo.jwplayer.events.PlaylistEvent.JWPLAYER_PLAYLIST_LOADED
19         */
20        [Event(name="jwplayerPlaylistLoaded", type="com.longtailvideo.jwplayer.events.PlaylistEvent")]
21       
22       
23        /**
24         * Sent when the playlist has been updated.
25         *
26         * @eventType com.longtailvideo.jwplayer.events.PlaylistEvent.JWPLAYER_PLAYLIST_UPDATED
27         */
28        [Event(name="jwplayerPlaylistUpdated", type="com.longtailvideo.jwplayer.events.PlaylistEvent")]
29       
30       
31        /**
32         * Sent when the playlist's current item has changed.
33         *
34         * @eventType com.longtailvideo.jwplayer.events.PlaylistEvent.JWPLAYER_PLAYLIST_ITEM
35         */
36        [Event(name="jwplayerPlaylistItem", type="com.longtailvideo.jwplayer.events.PlaylistEvent")]
37       
38       
39        /**
40         * Sent when an error ocurred when loading or parsing the playlist
41         *
42         * @eventType com.longtailvideo.jwplayer.events.PlayerEvent.JWPLAYER_ERROR
43         */
44        [Event(name="jwplayerError", type = "com.longtailvideo.jwplayer.events.PlayerEvent")]
45       
46       
47        public class Playlist extends GlobalEventDispatcher implements IPlaylist {
48                /** An array holding all of the PlaylistItem objects **/
49                private var list:Array;
50                /** The current playlist index **/
51                private var index:Number;
52                /** Keep track of the last playlistItem, so we can send a PLAYLIST_ITEM event at the correct time **/
53                private var lastItem:PlaylistItem = null;
54                /** AssetLoader to grab playlist XML files **/
55                private var playlistLoader:AssetLoader;
56               
57                /**
58                 * Constructor
59                 */
60                public function Playlist() {
61                        list = [];
62                        index = -1;
63                        playlistLoader = new AssetLoader();
64                        playlistLoader.addEventListener(Event.COMPLETE, playlistLoaded);
65                        playlistLoader.addEventListener(ErrorEvent.ERROR, playlistLoadError);
66                }
67               
68               
69                /**
70                 * @inheritDoc
71                 */
72                public function load(newPlaylist:Object):void {
73                        var newList:Array = [];
74                        if (newPlaylist is Array) {
75                                for (var i:Number = 0; i < (newPlaylist as Array).length; i++) {
76                                        if (!(newPlaylist[i] is PlaylistItem)) {
77                                                var newItem:PlaylistItem = new PlaylistItem(newPlaylist[i]);
78                                                newPlaylist[i] = newItem;
79                                        }
80                                        try {
81                                                if ((newPlaylist[i] as PlaylistItem).file) {
82                                                        newList.push(newPlaylist[i] as PlaylistItem);
83                                                }
84                                        } catch (e:Error) {
85                                        }
86                                }
87                        } else if (newPlaylist is PlaylistItem) {
88                                var pli:PlaylistItem = newPlaylist as PlaylistItem;
89                                if (JWParser.getProvider(pli)) {
90                                        newList.push(pli);
91                                } else {
92                                        load(pli.file);
93                                        return;
94                                }
95                        } else if (newPlaylist is Playlist) {
96                                for (i = 0; i < (newPlaylist as Playlist).length; i++) {
97                                        newList.push((newPlaylist as Playlist).getItemAt(i));
98                                }
99                        } else if (newPlaylist is String && newPlaylist != "") {
100                                playlistLoader.load(String(newPlaylist), XML);
101                                return;
102                        } else {
103                                playlistError("Incorrect playlist type");
104                                return;
105                        }
106                        if (newList.length > 0) {
107                                for each(var item:PlaylistItem in newList) {
108                                        if (!item.provider) {
109                                                item.provider = JWParser.getProvider(item);
110                                        }
111                                        if (item.provider == "youtube" && !item.image) {
112                                                item.image = 'http://i.ytimg.com/vi/' + YouTubeMediaProvider.getID(item.file) + '/0.jpg';
113                                        }
114                                }
115                                list = newList;
116                                index = 0;
117                                dispatchEvent(new PlaylistEvent(PlaylistEvent.JWPLAYER_PLAYLIST_LOADED, this));
118                        } else {
119                                dispatchEvent(new PlayerEvent(PlayerEvent.JWPLAYER_ERROR, "Loaded playlist is empty"));
120                        }
121                        return;
122                }
123               
124               
125                protected function playlistLoaded(evt:Event):void {
126                        var loadedXML:XML = playlistLoader.loadedObject as XML;
127                        var parser:IPlaylistParser = ParserFactory.getParser(loadedXML);
128                        if (parser) {
129                                var playlistItems:Array = parser.parse(loadedXML);
130                                if (playlistItems.length > 0) {
131                                        load(playlistItems);
132                                } else {
133                                        playlistError("XML could not be parsed or playlist was empty");
134                                }
135                        } else {
136                                playlistError("Playlist file did not contain a valid playlist");
137                        }
138                }
139               
140               
141                protected function playlistLoadError(evt:ErrorEvent):void {
142                        playlistError(evt.text);
143                }
144               
145               
146                protected function playlistError(message:String):void {
147                        if (message.indexOf("Error #2048") >= 0) {
148                                dispatchEvent(new PlayerEvent(PlayerEvent.JWPLAYER_ERROR, "Playlist could not be loaded due to crossdomain policy restrictions."));
149                        } else {
150                                dispatchEvent(new PlayerEvent(PlayerEvent.JWPLAYER_ERROR, "Playlist could not be loaded: " + message));
151                        }
152                }
153               
154               
155                /**
156                 * @inheritDoc
157                 */
158                public function getItemAt(idx:Number):PlaylistItem {
159                        try {
160                                return list[idx];
161                        } catch (e:Error) {
162                        }
163                        return null;
164                }
165               
166               
167                /**
168                 * @inheritDoc
169                 */
170                public function insertItem(itm:PlaylistItem, idx:Number = -1):void {
171                        if (idx >= 0 && idx < list.length) {
172                                list.splice(idx, 0, itm);
173                        } else {
174                                list.push(itm);
175                        }
176                        dispatchEvent(new PlaylistEvent(PlaylistEvent.JWPLAYER_PLAYLIST_UPDATED, this));
177                        if (index < 0) {
178                                currentIndex = list.length - 1;
179                        }
180                }
181               
182               
183                /**
184                 * @inheritDoc
185                 */
186                public function removeItemAt(idx:Number):void {
187                        if (idx >= 0 && idx < list.length && list.length > 0) {
188                                list.splice(idx, 1);
189                                dispatchEvent(new PlaylistEvent(PlaylistEvent.JWPLAYER_PLAYLIST_UPDATED, this));
190                        }
191                        if (index >= list.length) {
192                                currentIndex = list.length - 1;
193                        }
194                }
195               
196               
197                /**
198                 * @inheritDoc
199                 */
200                public function get currentIndex():Number {
201                        return index;
202                }
203               
204               
205                /**
206                 * @inheritDoc
207                 */
208                public function set currentIndex(idx:Number):void {
209                        if (idx > list.length) idx = 0;
210                        if (idx >= 0) {
211                                index = idx;
212                                if (getItemAt(idx) != lastItem) {
213                                        lastItem = currentItem;
214                                        dispatchEvent(new PlaylistEvent(PlaylistEvent.JWPLAYER_PLAYLIST_ITEM, this));
215                                }
216                        } else {
217                                lastItem = null;
218                                index = -1;
219                        }
220                }
221               
222               
223                /**
224                 * @inheritDoc
225                 */
226                public function get currentItem():PlaylistItem {
227                        return index >= 0 ? getItemAt(index) : null;
228                }
229               
230                /**
231                 * @inheritDoc
232                 */
233                public function get length():Number {
234                        return list.length;
235                }
236               
237                /**
238                 * @inheritDoc
239                 **/
240                public function contains(item:PlaylistItem):Boolean {
241                        for (var i:Number=0; i < length; i++) {
242                                if (getItemAt(i) == item) return true;
243                        }
244                        return false;
245                }
246        }
247}
Note: See TracBrowser for help on using the repository browser.