| 1 | package 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 | list = newList; |
|---|
| 107 | index = 0; |
|---|
| 108 | dispatchEvent(new PlaylistEvent(PlaylistEvent.JWPLAYER_PLAYLIST_LOADED, this)); |
|---|
| 109 | } else { |
|---|
| 110 | dispatchEvent(new PlayerEvent(PlayerEvent.JWPLAYER_ERROR, "Loaded playlist is empty")); |
|---|
| 111 | } |
|---|
| 112 | return; |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | protected function playlistLoaded(evt:Event):void { |
|---|
| 117 | var loadedXML:XML = playlistLoader.loadedObject as XML; |
|---|
| 118 | var parser:IPlaylistParser = ParserFactory.getParser(loadedXML); |
|---|
| 119 | if (parser) { |
|---|
| 120 | var playlistItems:Array = parser.parse(loadedXML); |
|---|
| 121 | if (playlistItems.length > 0) { |
|---|
| 122 | load(playlistItems); |
|---|
| 123 | } else { |
|---|
| 124 | playlistError("XML could not be parsed or playlist was empty"); |
|---|
| 125 | } |
|---|
| 126 | } else { |
|---|
| 127 | playlistError("Playlist file did not contain a valid playlist"); |
|---|
| 128 | } |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | |
|---|
| 132 | protected function playlistLoadError(evt:ErrorEvent):void { |
|---|
| 133 | playlistError(evt.text); |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | |
|---|
| 137 | protected function playlistError(message:String):void { |
|---|
| 138 | dispatchEvent(new PlayerEvent(PlayerEvent.JWPLAYER_ERROR, "Playlist could not be loaded: " + message)); |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | |
|---|
| 142 | /** |
|---|
| 143 | * @inheritDoc |
|---|
| 144 | */ |
|---|
| 145 | public function getItemAt(idx:Number):PlaylistItem { |
|---|
| 146 | try { |
|---|
| 147 | return list[idx]; |
|---|
| 148 | } catch (e:Error) { |
|---|
| 149 | } |
|---|
| 150 | return null; |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | /** |
|---|
| 155 | * @inheritDoc |
|---|
| 156 | */ |
|---|
| 157 | public function insertItem(itm:PlaylistItem, idx:Number = -1):void { |
|---|
| 158 | if (idx >= 0 && idx < list.length) { |
|---|
| 159 | list.splice(idx, 0, itm); |
|---|
| 160 | } else { |
|---|
| 161 | list.push(itm); |
|---|
| 162 | } |
|---|
| 163 | dispatchEvent(new PlaylistEvent(PlaylistEvent.JWPLAYER_PLAYLIST_UPDATED, this)); |
|---|
| 164 | if (index < 0) { |
|---|
| 165 | currentIndex = list.length - 1; |
|---|
| 166 | } |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | |
|---|
| 170 | /** |
|---|
| 171 | * @inheritDoc |
|---|
| 172 | */ |
|---|
| 173 | public function removeItemAt(idx:Number):void { |
|---|
| 174 | if (idx >= 0 && idx < list.length && list.length > 0) { |
|---|
| 175 | list.splice(idx, 1); |
|---|
| 176 | dispatchEvent(new PlaylistEvent(PlaylistEvent.JWPLAYER_PLAYLIST_UPDATED, this)); |
|---|
| 177 | } |
|---|
| 178 | if (index >= list.length) { |
|---|
| 179 | currentIndex = list.length - 1; |
|---|
| 180 | } |
|---|
| 181 | } |
|---|
| 182 | |
|---|
| 183 | |
|---|
| 184 | /** |
|---|
| 185 | * @inheritDoc |
|---|
| 186 | */ |
|---|
| 187 | public function get currentIndex():Number { |
|---|
| 188 | return index; |
|---|
| 189 | } |
|---|
| 190 | |
|---|
| 191 | |
|---|
| 192 | /** |
|---|
| 193 | * @inheritDoc |
|---|
| 194 | */ |
|---|
| 195 | public function set currentIndex(idx:Number):void { |
|---|
| 196 | if (idx > list.length) idx = 0; |
|---|
| 197 | if (idx >= 0) { |
|---|
| 198 | index = idx; |
|---|
| 199 | if (getItemAt(idx) != lastItem) { |
|---|
| 200 | lastItem = currentItem; |
|---|
| 201 | dispatchEvent(new PlaylistEvent(PlaylistEvent.JWPLAYER_PLAYLIST_ITEM, this)); |
|---|
| 202 | } |
|---|
| 203 | } else { |
|---|
| 204 | lastItem = null; |
|---|
| 205 | index = -1; |
|---|
| 206 | } |
|---|
| 207 | } |
|---|
| 208 | |
|---|
| 209 | |
|---|
| 210 | /** |
|---|
| 211 | * @inheritDoc |
|---|
| 212 | */ |
|---|
| 213 | public function get currentItem():PlaylistItem { |
|---|
| 214 | return index >= 0 ? getItemAt(index) : null; |
|---|
| 215 | } |
|---|
| 216 | |
|---|
| 217 | /** |
|---|
| 218 | * @inheritDoc |
|---|
| 219 | */ |
|---|
| 220 | public function get length():Number { |
|---|
| 221 | return list.length; |
|---|
| 222 | } |
|---|
| 223 | |
|---|
| 224 | /** |
|---|
| 225 | * @inheritDoc |
|---|
| 226 | **/ |
|---|
| 227 | public function contains(item:PlaylistItem):Boolean { |
|---|
| 228 | for (var i:Number=0; i < length; i++) { |
|---|
| 229 | if (getItemAt(i) == item) return true; |
|---|
| 230 | } |
|---|
| 231 | return false; |
|---|
| 232 | } |
|---|
| 233 | } |
|---|
| 234 | } |
|---|