开发者

How to cancel 'seeking' state of Flex3 VideoDisplay class

I have a VideoDisplay instance playing some video. When I click on the video slider (also my component) the property videoDisplay.playheadTime is set and the videoDisplay.state goes from 'playing' into a 'seeking' state for a brief moment (the videoDisplay seeks for a new position and then plays the video again). Intended bevaiour.

But if I'm (or an开发者_StackOverflow社区y random user) fast enough, I can set the playheadTime again while the player is still in 'seeking' state. When repeated several times every click is enqueued and the videoDisplay jump on every place of the video I have clicked(this is happening in an interval about 10-15 second after my last click). When I use live dragging the videoDisplay, overwhelmed by seekings, goes into 'error' state.

My question is - is there any way to cancel seeking state of the VideoDisplay class? For example player is in 'seeking' state, I set playheadTime, and the player forgets about last seeking and try to find the new place of the video.

I will downvote pointless answers like 'Use the Flex4 VideoPlayer class'!


One possible way is wrap the video display in a component and manage the seek a little better yourself. So if someone calls seek, make sure that the video is not currently seeking, if so, then wait till the current operation is complete before proceeding to the new one. If the user tries to seek again, discard all currently pending operations and make the latest one the next operation. Working on this exact problem right now.... Here's the code:

public function Seek(nSeconds:Number, bPlayAfter:Boolean):void
{ 
    trace("Player Seek: "+ nSeconds);
    var objSeekComand:VideoPlayerSeekCommand = new VideoPlayerSeekCommand(ucPlayer, nSeconds, bPlayAfter);
    ProcessCommand(objSeekComand);
}

protected function ProcessCommand(objCommand:ICommand):void
{
    if(_objCurrentCommand != null)
    {
        _objCurrentCommand.Abort();
    }

    _objCurrentCommand = objCommand
    objCommand.SignalCommandComplete.add(OnCommandComplete);
    objCommand.Execute();
}

Here's the Command

public class VideoPlayerSeekCommand extends CommandBase
    {
        private var _ucVideoDisplay:VideoDisplay;
        private var _nSeekPoint:Number;
        private var _bPlayAfterSeek:Boolean;
        private var _bIsExecuting:Boolean;

        public function VideoPlayerSeekCommand(ucVideoDisplay:VideoDisplay, nSeekPointInSeconds:Number, bPlayAfterSeek:Boolean,  fAutoAttachSignalHandler:Function = null)
        {
            _ucVideoDisplay = ucVideoDisplay;
            _nSeekPoint = nSeekPointInSeconds;
            _bPlayAfterSeek = bPlayAfterSeek;

            super(fAutoAttachSignalHandler);
        }

        override public function Execute():void
        {
            //First check if we are playing, and puase if needed
            _bIsExecuting = true;
            if(_ucVideoDisplay.playing == true)
            {
                _ucVideoDisplay.addEventListener(MediaPlayerStateChangeEvent.MEDIA_PLAYER_STATE_CHANGE, OnPlayerStateChangedFromPlay, false, 0, true);
                _ucVideoDisplay.pause();
            }
            else
            {
                DoSeek();
            }

        }

        protected function OnPlayerStateChangedFromPlay(event:MediaPlayerStateChangeEvent):void
        {
            _ucVideoDisplay.removeEventListener(MediaPlayerStateChangeEvent.MEDIA_PLAYER_STATE_CHANGE, OnPlayerStateChangedFromPlay);
            if(_bIsExecuting == true)
            {
                if(_ucVideoDisplay.playing == false)
                {
                    DoSeek();
                }
                else
                {
                    throw new Error("VideoPlayerSeekAndPlayCommand - OnPlayerStateChangedFromPlay error");
                }
            }
        }

        private function DoSeek():void
        {
            if(_bIsExecuting == true)
            {
                _ucVideoDisplay.seek(_nSeekPoint);
                CheckSeekComplete();
            }
        }

        private function CheckSeekComplete():void
        {
            if(_bIsExecuting == true)
            {
                if (Math.abs( _ucVideoDisplay.currentTime - _nSeekPoint) < 2)
                {
                    if(_bPlayAfterSeek == true)
                    {
                        _ucVideoDisplay.play();
                    }
                    DispatchAndDestroy();
                }
                else
                {
                    CoreUtils.CallLater(CheckSeekComplete, .07);
                }
            }
        }

        override public function Abort():void
        {
            _bIsExecuting = false;
            SignalCommandComplete.removeAll();
        }
    }

Im Using AS3 Signals here instead of events, and the CoreUtils.Call later you can use setInterval, or a Timer. But the idea is to not call seek until the video is paused, and to keep track of when the seek is complete.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜