| 1 | package com.longtailvideo.jwplayer.model { |
|---|
| 2 | import com.longtailvideo.jwplayer.events.GlobalEventDispatcher; |
|---|
| 3 | import com.longtailvideo.jwplayer.events.MediaEvent; |
|---|
| 4 | import com.longtailvideo.jwplayer.events.PlayerEvent; |
|---|
| 5 | import com.longtailvideo.jwplayer.events.PlayerStateEvent; |
|---|
| 6 | import com.longtailvideo.jwplayer.media.MediaProvider; |
|---|
| 7 | import com.longtailvideo.jwplayer.media.HTTPMediaProvider; |
|---|
| 8 | import com.longtailvideo.jwplayer.media.ImageMediaProvider; |
|---|
| 9 | import com.longtailvideo.jwplayer.media.RTMPMediaProvider; |
|---|
| 10 | import com.longtailvideo.jwplayer.media.SoundMediaProvider; |
|---|
| 11 | import com.longtailvideo.jwplayer.media.VideoMediaProvider; |
|---|
| 12 | import com.longtailvideo.jwplayer.media.YouTubeMediaProvider; |
|---|
| 13 | import com.longtailvideo.jwplayer.player.PlayerState; |
|---|
| 14 | |
|---|
| 15 | import flash.events.Event; |
|---|
| 16 | |
|---|
| 17 | /** |
|---|
| 18 | * Fired when a portion of the current media has been loaded into the buffer. |
|---|
| 19 | * |
|---|
| 20 | * @eventType com.longtailvideo.jwplayer.events.MediaEvent.JWPLAYER_MEDIA_BUFFER |
|---|
| 21 | */ |
|---|
| 22 | [Event(name="jwplayerMediaBuffer", type="com.longtailvideo.jwplayer.events.MediaEvent")] |
|---|
| 23 | /** |
|---|
| 24 | * Fired when the buffer is full. |
|---|
| 25 | * |
|---|
| 26 | * @eventType com.longtailvideo.jwplayer.events.MediaEvent.JWPLAYER_MEDIA_BUFFER_FULL |
|---|
| 27 | */ |
|---|
| 28 | [Event(name="jwplayerMediaBufferFull", type="com.longtailvideo.jwplayer.events.MediaEvent")] |
|---|
| 29 | /** |
|---|
| 30 | * Fired if an error occurs in the course of media playback. |
|---|
| 31 | * |
|---|
| 32 | * @eventType com.longtailvideo.jwplayer.events.MediaEvent.JWPLAYER_MEDIA_ERROR |
|---|
| 33 | */ |
|---|
| 34 | [Event(name="jwplayerMediaError", type="com.longtailvideo.jwplayer.events.MediaEvent")] |
|---|
| 35 | /** |
|---|
| 36 | * Fired after the MediaProvider has loaded an item into memory. |
|---|
| 37 | * |
|---|
| 38 | * @eventType com.longtailvideo.jwplayer.events.MediaEvent.JWPLAYER_MEDIA_LOADED |
|---|
| 39 | */ |
|---|
| 40 | [Event(name="jwplayerMediaLoaded", type="com.longtailvideo.jwplayer.events.MediaEvent")] |
|---|
| 41 | /** |
|---|
| 42 | * Sent after a load() command has completed |
|---|
| 43 | * |
|---|
| 44 | * @eventType com.longtailvideo.jwplayer.events.MediaEvent.JWPLAYER_MEDIA_TIME |
|---|
| 45 | */ |
|---|
| 46 | [Event(name="jwplayerMediaTime", type="com.longtailvideo.jwplayer.events.MediaEvent")] |
|---|
| 47 | /** |
|---|
| 48 | * Sends the position and duration of the currently playing media |
|---|
| 49 | * |
|---|
| 50 | * @eventType com.longtailvideo.jwplayer.events.MediaEvent.JWPLAYER_MEDIA_VOLUME |
|---|
| 51 | */ |
|---|
| 52 | [Event(name="jwplayerMediaVolume", type="com.longtailvideo.jwplayer.events.MediaEvent")] |
|---|
| 53 | /** |
|---|
| 54 | * Fired when the currently playing media has completed its playback |
|---|
| 55 | * |
|---|
| 56 | * @eventType com.longtailvideo.jwplayer.events.MediaEvent.JWPLAYER_MEDIA_COMPLETE |
|---|
| 57 | */ |
|---|
| 58 | [Event(name="jwplayerMediaComplete", type="com.longtailvideo.jwplayer.events.MediaEvent")] |
|---|
| 59 | /** |
|---|
| 60 | * Sent when the playback state has changed. |
|---|
| 61 | * |
|---|
| 62 | * @eventType com.longtailvideo.jwplayer.events.PlayerStateEvent.JWPLAYER_PLAYER_STATE |
|---|
| 63 | */ |
|---|
| 64 | [Event(name="jwplayerPlayerState", type="com.longtailvideo.jwplayer.events.PlayerStateEvent")] |
|---|
| 65 | /** |
|---|
| 66 | * Fired if an error has occurred in the model. |
|---|
| 67 | * |
|---|
| 68 | * @eventType com.longtailvideo.jwplayer.events.PlayerEvent.JWPLAYER_ERROR |
|---|
| 69 | */ |
|---|
| 70 | [Event(name="jwplayerError", type = "com.longtailvideo.jwplayer.events.PlayerEvent")] |
|---|
| 71 | |
|---|
| 72 | /** |
|---|
| 73 | * @author Pablo Schklowsky |
|---|
| 74 | */ |
|---|
| 75 | public class Model extends GlobalEventDispatcher { |
|---|
| 76 | private var _config:PlayerConfig; |
|---|
| 77 | private var _playlist:IPlaylist; |
|---|
| 78 | |
|---|
| 79 | private var _fullscreen:Boolean = false; |
|---|
| 80 | |
|---|
| 81 | private var _currentMedia:MediaProvider; |
|---|
| 82 | |
|---|
| 83 | private var _mediaSources:Object; |
|---|
| 84 | |
|---|
| 85 | /** Constructor **/ |
|---|
| 86 | public function Model() { |
|---|
| 87 | _playlist = new Playlist(); |
|---|
| 88 | _config = new PlayerConfig(); |
|---|
| 89 | _mediaSources = {}; |
|---|
| 90 | //TODO: Set initial mute state based on user configuration |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | /** The player config object **/ |
|---|
| 94 | public function get config():PlayerConfig { |
|---|
| 95 | return _config; |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | public function set config(conf:PlayerConfig):void { |
|---|
| 99 | _config = conf; |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | /** The currently loaded MediaProvider **/ |
|---|
| 103 | public function get media():MediaProvider { |
|---|
| 104 | return _currentMedia; |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | /** |
|---|
| 108 | * The current player state |
|---|
| 109 | */ |
|---|
| 110 | public function get state():String { |
|---|
| 111 | return _currentMedia ? _currentMedia.state : PlayerState.IDLE; |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | /** |
|---|
| 115 | * The loaded playlist |
|---|
| 116 | */ |
|---|
| 117 | public function get playlist():IPlaylist { |
|---|
| 118 | return _playlist; |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | /** The current fullscreen state of the player **/ |
|---|
| 122 | public function get fullscreen():Boolean { |
|---|
| 123 | return _config.fullscreen; |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | public function set fullscreen(b:Boolean):void { |
|---|
| 127 | _config.fullscreen = b; |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | /** The current mute state of the player **/ |
|---|
| 131 | public function get mute():Boolean { |
|---|
| 132 | return _config.mute; |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | public function set mute(b:Boolean):void { |
|---|
| 136 | _config.mute = b; |
|---|
| 137 | _currentMedia.mute(b); |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | public function setupMediaProviders():void { |
|---|
| 141 | setMediaProvider('default', new MediaProvider()); |
|---|
| 142 | setMediaProvider('video', new VideoMediaProvider()); |
|---|
| 143 | setMediaProvider('http', new HTTPMediaProvider()); |
|---|
| 144 | setMediaProvider('rtmp', new RTMPMediaProvider()); |
|---|
| 145 | setMediaProvider('sound', new SoundMediaProvider()); |
|---|
| 146 | setMediaProvider('image', new ImageMediaProvider()); |
|---|
| 147 | setMediaProvider('youtube', new YouTubeMediaProvider()); |
|---|
| 148 | |
|---|
| 149 | setActiveMediaProvider('default'); |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | /** |
|---|
| 153 | * Whether the Model has a MediaProvider handler for a given type. |
|---|
| 154 | */ |
|---|
| 155 | public function hasMediaProvider(type:String):Boolean { |
|---|
| 156 | return (_mediaSources[url2type(type)] is MediaProvider); |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | /** |
|---|
| 160 | * Add a MediaProvider to the list of available sources. |
|---|
| 161 | */ |
|---|
| 162 | public function setMediaProvider(type:String, provider:MediaProvider):void { |
|---|
| 163 | if (!hasMediaProvider(type)) { |
|---|
| 164 | _mediaSources[url2type(type)] = provider; |
|---|
| 165 | provider.initializeMediaProvider(config); |
|---|
| 166 | } |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | public function setActiveMediaProvider(type:String):Boolean { |
|---|
| 170 | if (!hasMediaProvider(type)) |
|---|
| 171 | type = "video"; |
|---|
| 172 | |
|---|
| 173 | var newMedia:MediaProvider = _mediaSources[url2type(type)] as MediaProvider; |
|---|
| 174 | |
|---|
| 175 | if (_currentMedia != newMedia) { |
|---|
| 176 | if (_currentMedia) { |
|---|
| 177 | _currentMedia.stop(); |
|---|
| 178 | _currentMedia.removeGlobalListener(forwardEvents); |
|---|
| 179 | } |
|---|
| 180 | newMedia.addGlobalListener(forwardEvents); |
|---|
| 181 | _currentMedia = newMedia; |
|---|
| 182 | } |
|---|
| 183 | |
|---|
| 184 | return true; |
|---|
| 185 | } |
|---|
| 186 | |
|---|
| 187 | |
|---|
| 188 | private function forwardEvents(evt:Event):void { |
|---|
| 189 | if (evt is PlayerEvent) { |
|---|
| 190 | if (evt.type == MediaEvent.JWPLAYER_MEDIA_ERROR) { |
|---|
| 191 | // Translate media error into player error. |
|---|
| 192 | dispatchEvent(new PlayerEvent(PlayerEvent.JWPLAYER_ERROR, (evt as MediaEvent).message)); |
|---|
| 193 | } else { |
|---|
| 194 | dispatchEvent(evt); |
|---|
| 195 | } |
|---|
| 196 | } |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | /** e.g. http://providers.longtailvideo.com/5/myProvider.swf --> myprovider **/ |
|---|
| 200 | private function url2type(type:String):String { |
|---|
| 201 | return type.substring(type.lastIndexOf("/") + 1, type.length).replace(".swf", "").toLowerCase(); |
|---|
| 202 | } |
|---|
| 203 | |
|---|
| 204 | } |
|---|
| 205 | } |
|---|