📄 netstreamcontrollingstreamprovider.as
字号:
* and dispatches the specified event. * * @param netStream * @param event the event that is dispatched after pausing, is <code>null</code> if * we are pausing silently */ protected function doPause(netStream:NetStream, event:ClipEvent = null):void { if (! netStream) return; netStream.pause(); if (event) { dispatchEvent(event); } } /** * Resumes the specified netStream. The implementation in this class calls <code>netStream.resume()</code> * and dispatches the specified event. * @param netStream * @param event the event that is dispatched after resuming */ protected function doResume(netStream:NetStream, event:ClipEvent):void { netStream.resume(); dispatchEvent(event); } /** * Silent seek mode. When enabled the SEEK event is not dispatched. * @see ClipEventType#SEEK */ protected final function set silentSeek(value:Boolean):void { _silentSeek = value; log.info("silent mode was set to " + _silentSeek); } protected final function get silentSeek():Boolean { return _silentSeek; } /** * Are we paused? */ protected final function get paused():Boolean { return _paused; } /** * Seeks the netStream to the specified target. The implementation in this class calls * <code>netStream.seek(seconds)</code>. Override if you need something different. * @param event the event that is dispatched after seeking successfully * @param netStream * @param seconds the seek target position */ protected function doSeek(event:ClipEvent, netStream:NetStream, seconds:Number):void { // the seek event is dispatched when we recevive the seek notification from netStream log.debug("calling netStream.seek(" + seconds + ")"); netStream.seek(seconds); } /** * Can we dispatch the start event now? This class uses this method every time * before it's about to dispatch the start event. The event is only dispatched * if this method returns <code>true</code>. * * @return <code>true</code> if the start event can be dispatched * @see ClipEventType#BEGIN */ protected function canDispatchBegin():Boolean { return true; } /** * Can we disppatch the onStreamNotFound ERROR event now? * @return <code>true</code> if the start event can be dispatched * * @see ClipEventType#ERROR */ protected function canDispatchStreamNotFound():Boolean { return true; } /** * Dispatches the specified event. */ protected final function dispatchEvent(event:ClipEvent):void { if (! event) return; log.debug("dispatching " + event); clip.dispatchEvent(event); } /** * Called when NetStatusEvents are received. */ protected function onNetStatus(event:NetStatusEvent):void { // can be overridden in subclasses } /** * Is the playback duration of current clip reached? */ protected function isDurationReached():Boolean { return Math.abs(getCurrentPlayheadTime(netStream) - clip.duration) <= 0.5; } /** * Gets the current playhead time. This should be overridden if the time * is not equl to netStream.time */ protected function getCurrentPlayheadTime(netStream:NetStream):Number { return netStream.time; } /** * The NetStream instance used by this provider. */ protected final function get netStream():NetStream { return _netStream; } /** * The current clip in the playlist. */ protected final function get clip():Clip { return _playlist.current; } /** * Should we pause on first frame after starting. * @see #load() the load() method has an autoPlay parameter that controls whether we stop on first frame or not */ protected final function get pauseAfterStart():Boolean { return _pauseAfterStart; } protected final function set pauseAfterStart(value:Boolean):void { _pauseAfterStart = value; } /** * Have we started streaming the playlist's current clip? */ protected function currentClipStarted():Boolean { return _startedClip == clip; } /** * The NetConnection object. */ protected function get netConnection():NetConnection { return _connection; } /** * Have we already received a NetStream.Play.Start from the NetStream */ protected function get started():Boolean { return _started; } /** * Previous seek target value in seconds. */ public function get seekTarget():Number { return _seekTarget; } /** * Override this to receive the plugin model. */ public function onConfig(model:PluginModel):void { } /** * Override this to receive the player instance. */ public function onLoad(player:Flowplayer):void { } /* ---- Private methods ----- */ /* -------------------------- */ private function dispatchError(error:ClipError, info:String):void { clip.dispatchError(error, info); } private function _onNetStatus(event:NetStatusEvent):void { log.info("_onNetStatus, code: ", event.info.code); if (_stopping) { log.info("_onNetStatus(), _stopping == true and will not process the event any further"); return; } if (event.info.code == "NetConnection.Connect.Success") { createNetStream(); start(null, clip, _pauseAfterStart); } else if (event.info.code == "NetStream.Buffer.Empty") { dispatchPlayEvent(ClipEventType.BUFFER_EMPTY); } else if (event.info.code == "NetStream.Buffer.Full") { dispatchPlayEvent(ClipEventType.BUFFER_FULL); } else if (event.info.code == "NetConnection.Connect.Success") { dispatchPlayEvent(ClipEventType.CONNECT); } else if (event.info.code == "NetStream.Play.Start") { if (! _paused && canDispatchBegin()) { log.debug("dispatching onBegin"); clip.dispatchEvent(new ClipEvent(ClipEventType.BEGIN)); } } else if (event.info.code == "NetStream.Play.Stop") {// dispatchPlayEvent(ClipEventType.STOP); } else if (event.info.code == "NetStream.Seek.Notify") { if (! silentSeek) { startSeekTargetWait(); } silentSeek = false; } else if (event.info.code == "NetStream.Seek.InvalidTime") { } else if (event.info.code == "NetStream.Play.StreamNotFound" || event.info.code == "NetConnection.Connect.Rejected" || event.info.code == "NetConnection.Connect.Failed") { if (canDispatchStreamNotFound()) { clip.dispatchError(ClipError.STREAM_NOT_FOUND, event.info.code); } } onNetStatus(event); } private function startSeekTargetWait():void { if (_seekTarget < 0) return; if (_seekTargetWaitTimer && _seekTargetWaitTimer.running) return; log.debug("starting seek target wait timer"); _seekTargetWaitTimer = new Timer(200); _seekTargetWaitTimer.addEventListener(TimerEvent.TIMER, onSeekTargetWait); _seekTargetWaitTimer.start(); } private function onSeekTargetWait(event:TimerEvent):void { if (time >= _seekTarget) { _seekTargetWaitTimer.stop(); log.debug("dispatching onSeek"); dispatchPlayEvent(ClipEventType.SEEK, _seekTarget); _seekTarget = -1; } } private function dispatchPlayEvent(playEvent:ClipEventType, info:Object = null):void { dispatchEvent(new ClipEvent(playEvent, info)); } private function doStop(event:ClipEvent, netStream:NetStream, closeStreamAndConnection:Boolean = false):void { log.debug("doStop"); _stopping = true; if (closeStreamAndConnection) { _startedClip = null; log.debug("doStop(), closing netStream and connection"); netStream.close(); if (_connection) { _connection.close(); _connection = null; } clip.setContent(null); } else { silentSeek = true; netStream.client = new NullNetStreamClient(); netStream.pause(); netStream.seek(0); } dispatchEvent(event); } private function createNetStream():void { _netStream = new NetStream(_connection); netStream.client = new NetStreamClient(clip, _config); _netStream.bufferTime = clip.bufferLength; _volumeController.netStream = _netStream; _netStream.addEventListener(NetStatusEvent.NET_STATUS, _onNetStatus); } protected function onMetaData(event:ClipEvent):void { log.info("in NetStreamControllingStremProvider.onStart"); clip.dispatch(ClipEventType.START, _pauseAfterStart); // some files require that we seek to the first frame only after receiving metadata // otherwise we will never receive the metadata if (_pauseAfterStart) { log.info("seeking to frame zero"); seek(null, 0); dispatchPlayEvent(ClipEventType.PAUSE); _pauseAfterStart = false; } } private function start(event:ClipEvent, clip:Clip, pauseAfterStart:Boolean = false):void { log.debug("start called with clip " + clip + ", pauseAfterStart " + pauseAfterStart); try { doLoad(event, _netStream, clip); _started = true; } catch (e:SecurityError) { dispatchError(ClipError.STREAM_LOAD_FAILED, "cannot access the video file (try loosening Flash security settings): " + e.message); } catch (e:IOError) { dispatchError(ClipError.STREAM_LOAD_FAILED, "cannot load the video file, incorrect URL?: " + e.message); } catch (e:Error) { dispatchError(ClipError.STREAM_LOAD_FAILED, "cannot play video: " + e.message); } if (pauseAfterStart) { log.info("pausing to first frame!"); doPause(_netStream, null);// _netStream.seek(0); } } public function onBWDone(... rest):void { } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -