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