source: trunk/fl5/src/com/longtailvideo/jwplayer/media/MediaProvider.as @ 523

Revision 523, 8.6 KB checked in by zach, 4 years ago (diff)
  • Added complete and errors functions and updated ImageMediaProvider, MediaProvider, RTMPMediaProvider, SoundMediaProvider, VideoMediaProvider, and YouTubeMediaProvider
  • Fixed YouTube stop / resume problem
  • Fixed RTMP stop / resume problem, buffering info problem, item duration problem
Line 
1package com.longtailvideo.jwplayer.media {
2        import com.longtailvideo.jwplayer.events.GlobalEventDispatcher;
3        import com.longtailvideo.jwplayer.events.IGlobalEventDispatcher;
4        import com.longtailvideo.jwplayer.events.MediaEvent;
5        import com.longtailvideo.jwplayer.events.PlayerStateEvent;
6        import com.longtailvideo.jwplayer.model.PlayerConfig;
7        import com.longtailvideo.jwplayer.model.PlaylistItem;
8        import com.longtailvideo.jwplayer.player.PlayerState;
9        import com.longtailvideo.jwplayer.utils.Stretcher;
10       
11        import flash.display.DisplayObject;
12        import flash.display.MovieClip;
13        import flash.display.Sprite;
14        import flash.events.Event;
15       
16        /**
17         * Fired when a portion of the current media has been loaded into the buffer.
18         *
19         * @eventType com.longtailvideo.jwplayer.events.MediaEvent.JWPLAYER_MEDIA_BUFFER
20         */
21        [Event(name="jwplayerMediaBuffer", type="com.longtailvideo.jwplayer.events.MediaEvent")]
22        /**
23         * Fired when the buffer is full.
24         *
25         * @eventType com.longtailvideo.jwplayer.events.MediaEvent.JWPLAYER_MEDIA_BUFFER_FULL
26         */
27        [Event(name="jwplayerMediaBufferFull", type="com.longtailvideo.jwplayer.events.MediaEvent")]
28        /**
29         * Fired if an error occurs in the course of media playback.
30         *
31         * @eventType com.longtailvideo.jwplayer.events.MediaEvent.JWPLAYER_MEDIA_ERROR
32         */
33        [Event(name="jwplayerMediaError", type="com.longtailvideo.jwplayer.events.MediaEvent")]
34        /**
35         * Fired after the MediaProvider has loaded an item into memory.
36         *
37         * @eventType com.longtailvideo.jwplayer.events.MediaEvent.JWPLAYER_MEDIA_LOADED
38         */
39        [Event(name="jwplayerMediaLoaded", type="com.longtailvideo.jwplayer.events.MediaEvent")]
40        /**
41         * @eventType com.longtailvideo.jwplayer.events.MediaEvent.JWPLAYER_MEDIA_TIME
42         */
43        [Event(name="jwplayerMediaTime", type="com.longtailvideo.jwplayer.events.MediaEvent")]
44        /**
45         * @eventType com.longtailvideo.jwplayer.events.MediaEvent.JWPLAYER_MEDIA_VOLUME
46         */
47        [Event(name="jwplayerMediaVolume", type="com.longtailvideo.jwplayer.events.MediaEvent")]
48        /**
49         * @eventType com.longtailvideo.jwplayer.events.PlayerStateEvent.JWPLAYER_PLAYER_STATE
50         */
51        [Event(name="jwplayerPlayerState", type="com.longtailvideo.jwplayer.events.PlayerStateEvent")]
52
53
54        public class MediaProvider extends Sprite implements IGlobalEventDispatcher {
55                /** Reference to the player configuration. **/
56                protected var _config:PlayerConfig;
57                /** Name of the MediaProvider **/
58                protected var _provider:String;
59                /** Reference to the currently active playlistitem. **/
60                protected var _item:PlaylistItem;
61                /** The current position inside the file. **/
62                protected var _position:Number;
63                /** The current volume of the audio output stream **/
64                protected var _volume:Number;
65                /** The playback state for the currently loaded media.  @see com.longtailvideo.jwplayer.model.ModelStates **/
66                protected var _state:String;
67                /** Clip containing graphical representation of the currently playing media **/
68                private var _media:MovieClip;
69                /** Most recent buffer data **/
70                private var bufferPercent:Number;
71                /** Handles event dispatching **/
72                protected var _dispatcher:GlobalEventDispatcher;
73
74                protected var _width:Number;
75                protected var _height:Number;
76               
77                public function MediaProvider(){
78                        _dispatcher = new GlobalEventDispatcher();
79                }
80               
81                public function initializeMediaProvider(cfg:PlayerConfig):void {
82                        _config = cfg;
83                        _state = PlayerState.IDLE;
84                }
85               
86                /**
87                 * Load a new playlist item
88                 * @param itm The playlistItem to load
89                 **/
90                public function load(itm:PlaylistItem):void {
91                        _item = itm;
92                        dispatchEvent(new MediaEvent(MediaEvent.JWPLAYER_MEDIA_LOADED));
93                }
94               
95               
96                /** Pause playback of the item. **/
97                public function pause():void {
98                        setState(PlayerState.PAUSED);
99                }
100               
101               
102                /** Resume playback of the item. **/
103                public function play():void {
104                        setState(PlayerState.PLAYING);
105                        if (_media) {
106                                _media.visible = true;
107                        }
108                }
109               
110               
111                /**
112                 * Seek to a certain position in the item.
113                 *
114                 * @param pos   The position in seconds.
115                 **/
116                public function seek(pos:Number):void {
117                        position = pos;
118                        sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_TIME, {position: position, duration:item.duration});
119                }
120               
121               
122                /** Stop playing and loading the item. **/
123                public function stop():void {
124                        setState(PlayerState.IDLE);
125                        position = 0;
126                        if (_media) {
127                                _media.visible = false;
128                        }
129                }
130               
131               
132                /**
133                 * Change the playback volume of the item.
134                 *
135                 * @param vol   The new volume (0 to 100).
136                 **/
137                public function setVolume(vol:Number):void {
138                        sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_VOLUME, {'volume': vol});
139                }
140
141
142                /**
143                 * Changes the mute state of the item.
144                 *
145                 * @param mute  The new mute state.
146                 **/
147                 public function mute(mute:Boolean):void {
148                        mute == true ? setVolume(0) : setVolume(_config.volume);
149                        sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_MUTE, {'mute': mute});
150                 }
151                 
152                 
153                 /** Completes video playback **/
154                 protected function complete():void {
155                        sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_COMPLETE);
156                        stop();
157                 }
158                 
159                 /** Puts the video into a buffer state **/
160                 protected function buffer():void {
161
162                 }
163               
164                /** Graphical representation of media **/
165                public function get display():DisplayObject {
166                        return _media;
167                }
168               
169               
170                /** Name of the MediaProvider. */
171                public function get provider():String {
172                        return _provider;
173                }
174               
175               
176                /**
177                 * Current state of the MediaProvider.
178                 * @see PlayerStates
179                 */
180                public function get state():String {
181                        return _state;
182                }
183               
184               
185                /** Currently playing PlaylistItem **/
186                public function get item():PlaylistItem {
187                        return _item;
188                }
189               
190               
191                /** Current position, in seconds **/
192                public function get position():Number {
193                        return _position;
194                }
195               
196                /** Set current position **/
197                public function set position(pos:Number):void {
198                        _position = pos;
199                }
200
201
202                /**
203                 * The current volume of the playing media
204                 * <p>Range: 0-100</p>
205                 */
206                public function get volume():Number {
207                        return _volume;
208                }
209               
210                /**
211                 * Resizes the display.
212                 *
213                 * @param width         The new width of the display.
214                 * @param height        The new height of the display.
215                 **/
216                 public function resize(width:Number, height:Number):void {
217                        _width = width;
218                        _height = height;
219                        if (_media) {
220                                Stretcher.stretch(_media, width, height, _config.stretching);
221                        }
222                 }
223               
224               
225                /**
226                 * Sets the current state to a new state and sends a PlayerStateEvent
227                 * @param newState A state from ModelStates.
228                 */
229                protected function setState(newState:String):void {
230                        if (state != newState) {
231                                var evt:PlayerStateEvent = new PlayerStateEvent(PlayerStateEvent.JWPLAYER_PLAYER_STATE, newState, state);
232                                _state = newState;
233                                dispatchEvent(evt);
234                        }
235                }
236               
237                /**
238                 * Sends a MediaEvent, simultaneously setting a property
239                 * @param type
240                 * @param property
241                 * @param value
242                 */
243                protected function sendMediaEvent(type:String, properties:Object = null):void {
244                        var newEvent:MediaEvent = new MediaEvent(type);
245                        for (var property:String in properties) {
246                                if (newEvent.hasOwnProperty(property)) {
247                                        newEvent[property] = properties[property];
248                                }
249                        }
250                        dispatchEvent(newEvent);
251                }
252               
253               
254                /** Dispatches buffer change notifications **/
255                protected function sendBufferEvent(bufferPercent:Number):void {
256                        if (bufferPercent != this.bufferPercent) {
257                                this.bufferPercent = bufferPercent;
258                                sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_BUFFER, {'bufferPercent': this.bufferPercent});
259                        }
260                }
261               
262               
263                /** Dispatches error notifications **/
264                protected function error(message:String):void {
265                        stop();
266                        sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_ERROR, {message: message});
267                }
268               
269               
270                /**
271                 * Gets a property from the player configuration
272                 *
273                 * @param property The property to be retrieved.
274                 * **/
275                protected function getConfigProperty(property:String):* {
276                        return _config.pluginConfig(provider)[property];
277                }
278               
279               
280                ///////////////////////////////////////////             
281                /// IGlobalEventDispatcher implementation
282                ///////////////////////////////////////////             
283                /**
284                 * @inheritDoc
285                 */
286                public function addGlobalListener(listener:Function):void {
287                        _dispatcher.addGlobalListener(listener);
288                }
289               
290               
291                /**
292                 * @inheritDoc
293                 */
294                public function removeGlobalListener(listener:Function):void {
295                        _dispatcher.removeGlobalListener(listener);
296                }
297               
298               
299                /**
300                 * @inheritDoc
301                 */
302                public override function dispatchEvent(event:Event):Boolean {
303                        _dispatcher.dispatchEvent(event);
304                        return super.dispatchEvent(event);
305                }
306               
307                protected function set media(m:DisplayObject):void {
308                        if (m) {
309                                _media = new MovieClip();
310                                _media.visible = false;
311                                _media.addChild(m);
312                                if (_width * _height > 0) {
313                                        Stretcher.stretch(_media, _width, _height, _config.stretching);
314                                }
315                        } else {
316                                _media = null;
317                        }
318                }
319               
320                protected function get media():DisplayObject {
321                        return _media;
322                }
323               
324        }
325}
Note: See TracBrowser for help on using the repository browser.