| [271] | 1 | package com.longtailvideo.jwplayer.model { |
|---|
| [751] | 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; |
|---|
| [552] | 9 | |
|---|
| [751] | 10 | import flash.events.ErrorEvent; |
|---|
| 11 | import flash.events.Event; |
|---|
| 12 | |
|---|
| 13 | |
|---|
| [271] | 14 | /** |
|---|
| [552] | 15 | * Sent when a playlist has been loaded. |
|---|
| [271] | 16 | * |
|---|
| [407] | 17 | * @eventType com.longtailvideo.jwplayer.events.PlaylistEvent.JWPLAYER_PLAYLIST_LOADED |
|---|
| [271] | 18 | */ |
|---|
| [552] | 19 | [Event(name="jwplayerPlaylistLoaded", type="com.longtailvideo.jwplayer.events.PlaylistEvent")] |
|---|
| 20 | |
|---|
| 21 | |
|---|
| [271] | 22 | /** |
|---|
| [552] | 23 | * Sent when the playlist has been updated. |
|---|
| [271] | 24 | * |
|---|
| [407] | 25 | * @eventType com.longtailvideo.jwplayer.events.PlaylistEvent.JWPLAYER_PLAYLIST_UPDATED |
|---|
| [271] | 26 | */ |
|---|
| [552] | 27 | [Event(name="jwplayerPlaylistUpdated", type="com.longtailvideo.jwplayer.events.PlaylistEvent")] |
|---|
| 28 | |
|---|
| 29 | |
|---|
| [271] | 30 | /** |
|---|
| [552] | 31 | * Sent when the playlist's current item has changed. |
|---|
| [271] | 32 | * |
|---|
| [407] | 33 | * @eventType com.longtailvideo.jwplayer.events.PlaylistEvent.JWPLAYER_PLAYLIST_ITEM |
|---|
| [271] | 34 | */ |
|---|
| [552] | 35 | [Event(name="jwplayerPlaylistItem", type="com.longtailvideo.jwplayer.events.PlaylistEvent")] |
|---|
| 36 | |
|---|
| 37 | |
|---|
| [318] | 38 | /** |
|---|
| [552] | 39 | * Sent when an error ocurred when loading or parsing the playlist |
|---|
| [318] | 40 | * |
|---|
| [407] | 41 | * @eventType com.longtailvideo.jwplayer.events.PlayerEvent.JWPLAYER_ERROR |
|---|
| [318] | 42 | */ |
|---|
| [564] | 43 | [Event(name="jwplayerError", type = "com.longtailvideo.jwplayer.events.PlayerEvent")] |
|---|
| [552] | 44 | |
|---|
| 45 | |
|---|
| 46 | public class Playlist extends GlobalEventDispatcher implements IPlaylist { |
|---|
| [751] | 47 | /** An array holding all of the PlaylistItem objects **/ |
|---|
| [271] | 48 | private var list:Array; |
|---|
| [751] | 49 | /** The current playlist index **/ |
|---|
| [271] | 50 | private var index:Number; |
|---|
| [751] | 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; |
|---|
| [762] | 53 | /** AssetLoader to grab playlist XML files **/ |
|---|
| 54 | private var playlistLoader:AssetLoader; |
|---|
| [552] | 55 | |
|---|
| [271] | 56 | /** |
|---|
| [552] | 57 | * Constructor |
|---|
| [271] | 58 | */ |
|---|
| 59 | public function Playlist() { |
|---|
| 60 | list = []; |
|---|
| 61 | index = -1; |
|---|
| [762] | 62 | playlistLoader = new AssetLoader(); |
|---|
| 63 | playlistLoader.addEventListener(Event.COMPLETE, playlistLoaded); |
|---|
| 64 | playlistLoader.addEventListener(ErrorEvent.ERROR, playlistLoadError); |
|---|
| [271] | 65 | } |
|---|
| [552] | 66 | |
|---|
| 67 | |
|---|
| [271] | 68 | /** |
|---|
| [552] | 69 | * @inheritDoc |
|---|
| [271] | 70 | */ |
|---|
| 71 | public function load(newPlaylist:Object):void { |
|---|
| [318] | 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 | } |
|---|
| [552] | 83 | } catch (e:Error) { |
|---|
| 84 | } |
|---|
| [318] | 85 | } |
|---|
| [475] | 86 | } else if (newPlaylist is PlaylistItem) { |
|---|
| [496] | 87 | var pli:PlaylistItem = newPlaylist as PlaylistItem; |
|---|
| [818] | 88 | if (JWParser.getProvider(pli)) { |
|---|
| [496] | 89 | newList.push(pli); |
|---|
| 90 | } else { |
|---|
| 91 | load(pli.file); |
|---|
| [597] | 92 | return; |
|---|
| [496] | 93 | } |
|---|
| [318] | 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 | } |
|---|
| [335] | 98 | } else if (newPlaylist is String && newPlaylist != "") { |
|---|
| 99 | playlistLoader.load(String(newPlaylist), XML); |
|---|
| 100 | return; |
|---|
| [318] | 101 | } else { |
|---|
| [335] | 102 | playlistError("Incorrect playlist type"); |
|---|
| [318] | 103 | return; |
|---|
| 104 | } |
|---|
| [510] | 105 | if (newList.length > 0) { |
|---|
| [977] | 106 | for each(var item:PlaylistItem in newList) { |
|---|
| 107 | if (!item.provider) { |
|---|
| 108 | item.provider = JWParser.getProvider(item); |
|---|
| 109 | } |
|---|
| 110 | } |
|---|
| [510] | 111 | list = newList; |
|---|
| [622] | 112 | index = 0; |
|---|
| [510] | 113 | dispatchEvent(new PlaylistEvent(PlaylistEvent.JWPLAYER_PLAYLIST_LOADED, this)); |
|---|
| [396] | 114 | } else { |
|---|
| 115 | dispatchEvent(new PlayerEvent(PlayerEvent.JWPLAYER_ERROR, "Loaded playlist is empty")); |
|---|
| 116 | } |
|---|
| [552] | 117 | return; |
|---|
| [271] | 118 | } |
|---|
| [552] | 119 | |
|---|
| 120 | |
|---|
| [335] | 121 | protected function playlistLoaded(evt:Event):void { |
|---|
| [762] | 122 | var loadedXML:XML = playlistLoader.loadedObject as XML; |
|---|
| [335] | 123 | var parser:IPlaylistParser = ParserFactory.getParser(loadedXML); |
|---|
| [737] | 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 | } |
|---|
| [335] | 131 | } else { |
|---|
| [737] | 132 | playlistError("Playlist file did not contain a valid playlist"); |
|---|
| [335] | 133 | } |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| [552] | 136 | |
|---|
| [335] | 137 | protected function playlistLoadError(evt:ErrorEvent):void { |
|---|
| 138 | playlistError(evt.text); |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| [552] | 141 | |
|---|
| [335] | 142 | protected function playlistError(message:String):void { |
|---|
| [1149] | 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 | } |
|---|
| [552] | 148 | } |
|---|
| 149 | |
|---|
| 150 | |
|---|
| [271] | 151 | /** |
|---|
| [552] | 152 | * @inheritDoc |
|---|
| [271] | 153 | */ |
|---|
| 154 | public function getItemAt(idx:Number):PlaylistItem { |
|---|
| 155 | try { |
|---|
| [552] | 156 | return list[idx]; |
|---|
| 157 | } catch (e:Error) { |
|---|
| 158 | } |
|---|
| [271] | 159 | return null; |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| [552] | 162 | |
|---|
| [271] | 163 | /** |
|---|
| [552] | 164 | * @inheritDoc |
|---|
| [271] | 165 | */ |
|---|
| [552] | 166 | public function insertItem(itm:PlaylistItem, idx:Number = -1):void { |
|---|
| [281] | 167 | if (idx >= 0 && idx < list.length) { |
|---|
| [271] | 168 | list.splice(idx, 0, itm); |
|---|
| 169 | } else { |
|---|
| 170 | list.push(itm); |
|---|
| 171 | } |
|---|
| [510] | 172 | dispatchEvent(new PlaylistEvent(PlaylistEvent.JWPLAYER_PLAYLIST_UPDATED, this)); |
|---|
| [281] | 173 | if (index < 0) { |
|---|
| 174 | currentIndex = list.length - 1; |
|---|
| [552] | 175 | } |
|---|
| [271] | 176 | } |
|---|
| [552] | 177 | |
|---|
| 178 | |
|---|
| [271] | 179 | /** |
|---|
| [552] | 180 | * @inheritDoc |
|---|
| [271] | 181 | */ |
|---|
| 182 | public function removeItemAt(idx:Number):void { |
|---|
| 183 | if (idx >= 0 && idx < list.length && list.length > 0) { |
|---|
| 184 | list.splice(idx, 1); |
|---|
| [510] | 185 | dispatchEvent(new PlaylistEvent(PlaylistEvent.JWPLAYER_PLAYLIST_UPDATED, this)); |
|---|
| [271] | 186 | } |
|---|
| [281] | 187 | if (index >= list.length) { |
|---|
| 188 | currentIndex = list.length - 1; |
|---|
| 189 | } |
|---|
| [271] | 190 | } |
|---|
| 191 | |
|---|
| [552] | 192 | |
|---|
| 193 | /** |
|---|
| 194 | * @inheritDoc |
|---|
| 195 | */ |
|---|
| [271] | 196 | public function get currentIndex():Number { |
|---|
| 197 | return index; |
|---|
| [552] | 198 | } |
|---|
| [271] | 199 | |
|---|
| [552] | 200 | |
|---|
| 201 | /** |
|---|
| 202 | * @inheritDoc |
|---|
| 203 | */ |
|---|
| [271] | 204 | public function set currentIndex(idx:Number):void { |
|---|
| [835] | 205 | if (idx > list.length) idx = 0; |
|---|
| 206 | if (idx >= 0) { |
|---|
| 207 | index = idx; |
|---|
| 208 | if (getItemAt(idx) != lastItem) { |
|---|
| [751] | 209 | lastItem = currentItem; |
|---|
| [510] | 210 | dispatchEvent(new PlaylistEvent(PlaylistEvent.JWPLAYER_PLAYLIST_ITEM, this)); |
|---|
| [835] | 211 | } |
|---|
| 212 | } else { |
|---|
| 213 | lastItem = null; |
|---|
| 214 | index = -1; |
|---|
| [281] | 215 | } |
|---|
| [552] | 216 | } |
|---|
| [271] | 217 | |
|---|
| [552] | 218 | |
|---|
| 219 | /** |
|---|
| 220 | * @inheritDoc |
|---|
| 221 | */ |
|---|
| [271] | 222 | public function get currentItem():PlaylistItem { |
|---|
| [396] | 223 | return index >= 0 ? getItemAt(index) : null; |
|---|
| [271] | 224 | } |
|---|
| 225 | |
|---|
| [552] | 226 | /** |
|---|
| 227 | * @inheritDoc |
|---|
| 228 | */ |
|---|
| [271] | 229 | public function get length():Number { |
|---|
| 230 | return list.length; |
|---|
| 231 | } |
|---|
| [751] | 232 | |
|---|
| [628] | 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 | } |
|---|
| [271] | 242 | } |
|---|
| 243 | } |
|---|