Problem with Video Scrubber causing error

Hi there, hopefully someone can help me work out this problem I’ve been having for the past two days!! :slight_smile:

I have a built my own AS3 Flash video player and the ‘scrubber’ bar appears to be causing an error message when dragged too far ahead (I assume because it’s dragging ahead to a part of the video which has yet to fully load).

The error message I’m getting is…


Error #2044: Unhandled NetStatusEvent:. level=error, code=NetStream.Seek.InvalidTime
    at VPlayer()

My VPlayer Class (which creates the video player) code is as follows…

NOTE: to find the scrubbing code look for the comment “// Handle the scrubbing through the video playback” and you’ll see that I’ve attempted to wrap the scrubbing code in a Try/Catch statement (which doesn’t seem to work as it doesn’t catch any errors so you can remove that Try/Catch statement if you feel it’s not needed)


package
{
    import flash.text.TextField;
    import flash.media.Video;
    import flash.net.NetConnection;
    import flash.net.NetStream;
    import flash.events.NetStatusEvent;
    import flash.text.TextFieldAutoSize;
    import flash.text.TextFormat;
    import flash.display.Sprite;
    import flash.display.MovieClip;
    import flash.display.StageDisplayState;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.events.NetStatusEvent;
    import flash.events.SecurityErrorEvent;
    import flash.events.AsyncErrorEvent;
    import flash.media.SoundTransform;
    import flash.geom.Rectangle;
    
    public class VPlayer extends Sprite 
    {
        private var _stream:NetStream;
        private var _play:MovieClip;
        private var _controls:MovieClip;
        private var _video:Video;
        private var _toolbar:Sprite;
        private var _thumb:Sprite;
        private var _track:Sprite;
        private var _scrubbing:Boolean;
        private var _playbackTime:TextField;
        private var _duration:uint;
        private var _TotalDuration;
        private var _SoundTransform:SoundTransform = new SoundTransform();
        private var _TextFormat:TextFormat = new TextFormat();
        
        public function VPlayer() 
        {
               _controls = new Controls();
            _play = new Play();
            _play.buttonMode = true;
            _play.addEventListener(MouseEvent.CLICK, function(){ 
                                                                       _stream.play("Video.flv");
                                                                       removeChild(_play);
                                                                    addChild(_controls);                                                                    
                                                                    addChild(_thumb);            
                                                                    addChild(_track);
                                                                    addChild(_playbackTime);
                                                                    _controls.bFullScreen.addEventListener(MouseEvent.CLICK, toggleFullScreen);
                                                                    _controls.bMuteSound.addEventListener(MouseEvent.CLICK, toggleSound);
                                                                    _controls.bPlayPause.addEventListener(MouseEvent.CLICK, togglePlayback);
                                                                    _controls.bPlayPause.buttonMode = true;
                                                                    _thumb.addEventListener(MouseEvent.MOUSE_DOWN, oMouseDown);
                                                                    _thumb.addEventListener(MouseEvent.MOUSE_UP, oMouseUp);
                                                                    _thumb.buttonMode = true;
                                                               })
            
            _video = new Video(419, 235);
            _playbackTime = new TextField();
            _playbackTime.x = 155;
            _playbackTime.y = 217;
            _playbackTime.autoSize = TextFieldAutoSize.LEFT;
            _duration = 0;
            
            var connection:NetConnection = new NetConnection();
            connection.connect(null);
            connection.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
            connection.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
            _stream = new NetStream(connection);
            
            var client:Object = new Object();
            client.onMetaData = onMetaData;
            
            _stream.client = client;
            _stream.bufferTime = 10;
            _video.attachNetStream(_stream);
            
            _track = new Sprite();
            _track.graphics.lineStyle();
            _track.graphics.drawRect(0, -2.5, 100, 5);
            
            _thumb = new Sprite();
            _thumb.graphics.lineStyle();
            _thumb.graphics.beginFill(0xCC0000);
            _thumb.graphics.drawRect(36, 221, 11, 11);
            _thumb.graphics.endFill();
            
            addChild(_video);
            addChild(_play);
            
            addEventListener(Event.ENTER_FRAME, oEnterFrame);
            _stream.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
        }
        
        private function toggleControls(e:MouseEvent):void
        {
            _controls.gotoAndPlay(2);
        }
        
        private function toggleFullScreen(e:MouseEvent):void
        {
            stage.displayState = StageDisplayState.FULL_SCREEN;
        }
        
        private function toggleSound(e:MouseEvent):void
        {
            switch(_SoundTransform.volume)
            {
                case 0:
                    _controls.bMuteSound.alpha = 1;
                    _SoundTransform.volume = 1;
                    break;
                    
                case 1:
                    _controls.bMuteSound.alpha = 0.3;
                    _SoundTransform.volume = 0;
                    break;
            }
            
            _stream.soundTransform = _SoundTransform;
        }
        
        private function togglePlayback(e:MouseEvent):void
        {
            switch(_controls.bPlayPause.currentFrame)
            {
                case 1:
                    _controls.bPlayPause.gotoAndStop(2);
                    break;
                    
                case 2:
                    _controls.bPlayPause.gotoAndStop(1);
                    break;
            }
            
            _stream.togglePause();
        }

        private function onMetaData(data:Object):void 
        {
            _duration = data.duration;            
            
            // GET THE HOURS
            var Hours = Math.floor(_duration / 3600);
            
            // GET THE MINUTES
            var Minutes = Math.floor((_duration - 3600 * Hours) / 60);
            
            // GET THE SECONDS
            var Seconds = (_duration - 3600 * Hours - 60 * Minutes)
            
            _TotalDuration = /*String(Hours) + ":" +*/ String(Minutes) + ":" + String(Seconds);
        }
        
        private function oEnterFrame(event:Event):void 
        {
            if(_duration > 0 && _stream.time > 0) 
            {
                _playbackTime.defaultTextFormat = _TextFormat;
                _TextFormat.color = 0xCC0000;
                _TextFormat.font = "Arial";
                _TextFormat.bold = true;
                _playbackTime.setTextFormat(_TextFormat);
                _playbackTime.text = generateTime(_stream.time) + " / " + _TotalDuration;
            }
            
            // Handle the scrubbing through the video playback
            if(_duration > 0) 
            {
                if(_scrubbing) 
                {
                    try
                    {
                        _stream.seek(_duration * _thumb.x / _track.width);
                    }
                    catch(err)
                    {
                        _playbackTime.text = err;
                    }
                }
                else 
                {
                    try
                    {
                        _thumb.x = _stream.time / _duration * _track.width;
                    }
                    catch(err)
                    {
                        _playbackTime.text = err;
                    }
                }
            }
        }
        
        function generateTime(nCurrentTime:Number):String
        {
            var nMinutes:String = (Math.floor(nCurrentTime / 60) < 10 ? "0" : "") + Math.floor(nCurrentTime / 60);
            var nSeconds:String = (Math.floor(nCurrentTime % 60) < 10 ? "0" : "") + Math.floor(nCurrentTime % 60);
            
            //Set Result 
            var sResult:String = nMinutes + ":" + nSeconds;
            
            //Return Value
            return sResult;
        }
        
        private function oMouseDown(e:MouseEvent):void 
        {
            _scrubbing = true;
            var rectangle:Rectangle = new Rectangle(0, 0, _track.width, 0);
            _thumb.startDrag(false, rectangle);
        }
        
        private function oMouseUp(e:MouseEvent):void 
        {
            _scrubbing = false;
            _thumb.stopDrag();
        }
        
        public function netStatusHandler(e:NetStatusEvent):void 
        {
            switch (e.info.code) 
            {
                case "NetStream.Seek.InvalidTime":
                    trace("You have seeked too far ahead, the video hasn't fully loaded yet");
                    _playbackTime.text = "You have seeked too far ahead, the video hasn't fully loaded yet"
                    break;
                case "NetStream.Play.StreamNotFound":
                    trace("Unable to locate video");
                    _playbackTime.text = "Unable to locate video"
                    break;
            }
        }
        
        private function securityErrorHandler(e:SecurityErrorEvent):void 
        {
            trace("securityErrorHandler: " + e);
        }
        
        private function asyncErrorHandler(e:AsyncErrorEvent):void 
        {
            trace("asyncErrorHandler; " + e);
        }
    }
}

Many thanks for any help!

Kind regards,
M.