source: trunk/fl5/src/com/longtailvideo/jwplayer/media/VideoMediaProvider.as @ 519

Revision 519, 6.1 KB checked in by zach, 4 years ago (diff)
  • Updating SoundMediaProvider to handle null channels
  • Updating VideoMediaProvider play / pause bug
  • Fixing YouTube buffering issue
Line 
1/**
2 * Wrapper for playback of progressively downloaded video.
3 **/
4package com.longtailvideo.jwplayer.media {
5        import com.longtailvideo.jwplayer.events.MediaEvent;
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.NetClient;
10       
11        import flash.events.*;
12        import flash.media.*;
13        import flash.net.*;
14        import flash.utils.*;
15       
16       
17        public class VideoMediaProvider extends MediaProvider {
18                /** Video object to be instantiated. **/
19                protected var video:Video;
20                /** NetConnection object for setup of the video stream. **/
21                protected var connection:NetConnection;
22                /** NetStream instance that handles the stream IO. **/
23                protected var stream:NetStream;
24                /** Sound control object. **/
25                protected var transformer:SoundTransform;
26                /** ID for the position interval. **/
27                protected var positionInterval:Number;
28                /** Load offset for bandwidth checking. **/
29                protected var loadTimer:Number;
30               
31               
32                /** Constructor; sets up the connection and display. **/
33                public function VideoMediaProvider() {
34                }
35               
36                public override function initializeMediaProvider(cfg:PlayerConfig):void {
37                        super.initializeMediaProvider(cfg);
38                        _provider = 'video';
39                        connection = new NetConnection();
40                        connection.connect(null);
41                        stream = new NetStream(connection);
42                        stream.addEventListener(NetStatusEvent.NET_STATUS, statusHandler);
43                        stream.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
44                        stream.addEventListener(AsyncErrorEvent.ASYNC_ERROR, errorHandler);
45                        stream.bufferTime = _config.bufferlength;
46                        stream.client = new NetClient(this);
47                        video = new Video(320, 240);
48                        video.smoothing = _config.smoothing;
49                        video.attachNetStream(stream);
50                        transformer = new SoundTransform();
51                }
52               
53               
54                /** Catch security errors. **/
55                protected function errorHandler(evt:ErrorEvent):void {
56                        stop();
57                        sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_ERROR, {message: evt.text});
58                }
59               
60               
61                /** Load content. **/
62                override public function load(itm:PlaylistItem):void {
63                        if (_item != itm || stream.bytesLoaded == 0) {
64                                _item = itm;
65                                media = video;
66                                stream.checkPolicyFile = true;
67                                stream.play(item.file);
68                        }
69                        positionInterval = setInterval(positionHandler, 200);
70                        loadTimer = setTimeout(loadTimerComplete, 3000);
71                        sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_LOADED);
72                        _config.mute == true ? setVolume(0) : setVolume(_config.volume);
73                        setState(PlayerState.BUFFERING);
74                        sendBufferEvent(0);
75                }
76                               
77               
78                /** timeout for checking the bitrate. **/
79                protected function loadTimerComplete():void {
80                        var obj:Object = new Object();
81                        obj.bandwidth = Math.round(stream.bytesLoaded / 1024 / 3 * 8);
82                        if (item.duration) {
83                                obj.bitrate = Math.round(stream.bytesTotal / 1024 * 8 / item.duration);
84                        }
85                        sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_META, {metadata: obj});
86                }
87               
88               
89                /** Get metadata information from netstream class. **/
90                public function onData(dat:Object):void {
91                        if (dat.width) {
92                                video.width = dat.width;
93                                video.height = dat.height;
94                                resize(_width, _height);
95                        }
96                        if (dat.duration) {
97                                _item.duration = dat.duration;
98                        }
99                        sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_META, {metadata: dat});
100                }
101               
102               
103                /** Pause playback. **/
104                override public function pause():void {
105                        stream.pause();
106                        super.pause();
107                }
108               
109               
110                /** Resume playing. **/
111                override public function play():void {
112                        if (!positionInterval) {
113                                positionInterval = setInterval(positionHandler, 100);
114                        }
115                        stream.resume();
116                        super.play();
117                }
118               
119               
120                /** Interval for the position progress **/
121                protected function positionHandler():void {
122                        position = Math.round(stream.time * 10) / 10;
123                        var bufferPercent:Number = stream.bytesTotal == 0 ? 0 : Math.round(stream.bytesLoaded / stream.bytesTotal * 100);
124                        var bufferFill:Number = stream.bufferTime == 0 ? 0 : Math.round(stream.bufferLength / stream.bufferTime * 100);
125                        if (bufferFill < 95 && position < Math.abs(item.duration - stream.bufferTime - 1)) {
126                                if (state == PlayerState.PLAYING && bufferFill < 25) {
127                                        stream.pause();
128                                        setState(PlayerState.BUFFERING);
129                                }
130                                sendBufferEvent(bufferPercent);
131                        } else if (bufferFill > 95 && state == PlayerState.BUFFERING) {
132                                sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_BUFFER_FULL);
133                        }
134                       
135                        if (state == PlayerState.BUFFERING){
136                                sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_BUFFER, {bufferPercent:bufferPercent});
137                        } else if (position < item.duration) {
138                                if (state == PlayerState.PLAYING && position >= 0) {
139                                        sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_TIME, {position: position, duration: item.duration, bufferPercent:bufferPercent});
140                                }
141                        } else if (item.duration > 0) {
142                                complete();
143                        }
144                }
145
146                private function complete():void {
147                        stream.pause();
148                        clearInterval(positionInterval);
149                        positionInterval = undefined;
150                        setState(PlayerState.IDLE);
151                        sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_COMPLETE);
152                        position = 0;
153                        stream.seek(position);
154                }
155               
156               
157                /** Seek to a new position. **/
158                override public function seek(pos:Number):void {
159                        super.seek(pos);
160                        clearInterval(positionInterval);
161                        positionInterval = undefined;
162                        stream.seek(position);
163                        play();
164                }
165               
166               
167                /** Receive NetStream status updates. **/
168                protected function statusHandler(evt:NetStatusEvent):void {
169                        switch (evt.info.code) {
170                                case "NetStream.Play.Stop":
171                                        complete();
172                                        break;
173                                case "NetStream.Play.StreamNotFound":
174                                        stop();
175                                        sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_ERROR, {message: 'Video not found or access denied: ' + item.file});
176                                        break;
177                        }
178                        sendMediaEvent(MediaEvent.JWPLAYER_MEDIA_META, {metadata: {status: evt.info.code}});
179                }
180               
181               
182                /** Destroy the video. **/
183                override public function stop():void {
184                        if (stream.bytesLoaded < stream.bytesTotal) {
185                                stream.close();
186                        } else {
187                                stream.pause();
188                                stream.seek(0);
189                        }
190                        loadTimer = undefined;
191                        clearInterval(positionInterval);
192                        positionInterval = undefined;
193                        super.stop();
194                }
195               
196               
197                /** Set the volume level. **/
198                override public function setVolume(vol:Number):void {
199                        transformer.volume = vol / 100;
200                        stream.soundTransform = transformer;
201                        super.setVolume(vol);
202                }
203        }
204}
Note: See TracBrowser for help on using the repository browser.