3 Replies Latest reply on Sep 28, 2009 3:48 PM by rmj134

    VideoPlayer complete event firing early

    RossRitchey Level 4

      I'm not sure why this is happening, but for some reason the complete event is firing about 5-10 seconds into my minutes long video.

       

      Here is the code for the VideoPage that loads the video and listens for the events. Does anything seem off to anyone?

       

      package com.applied.flex.pages
      {
           
           import com.applied.flex.events.PageEvent;
           
           import flash.events.ProgressEvent;
           
           import spark.components.VideoPlayer;
           import spark.events.VideoEvent;
      
           public class VideoPage extends BasePage
           {
                private var _video:VideoPlayer;
                
                private var _startTime:uint = 0;
                private var _endTime:uint;
                
                private var _cuePoints:Array;
                private var _displayedPoints:Array;
                
                private var resetting:Boolean = false;
                private var seekSafe:Boolean = true;
                
                public var readyToStart:Boolean = false;
                
                public function VideoPage(xml:XML,majorType:String,minorType:String,path:String)
                {
                     super(xml,majorType,minorType,path);
                     
                     _video = new VideoPlayer();
                     
                     if(xml..Video[0].@StartTime != undefined)
                     {
                          _startTime = xml..Video[0].@StartTime;
                     }
                     
                     if(xml..Video[0].@EndTime != undefined)
                     {
                          _endTime = xml..Video[0].@EndTime;
                     }
                     
                     _cuePoints = new Array();
                     
                     for each(var i:XML in _pageXML.Video.CuePoints.children())
                     {
                          _cuePoints.push(i.@OnTime);
                     }
                     
                     _video.addEventListener(VideoEvent.READY,doVideoReady);
                     _video.source = path;
                     
                     this.addChild(_video);
                }
                
                private function doVideoReady(e:VideoEvent):void
                {
                     _video.removeEventListener(VideoEvent.METADATA_RECEIVED,doVideoReady);
                     if(_pageXML..Video[0].@EndTime == undefined)
                     {
                          _endTime = _video.totalTime;
                     }
                     
                     readyToStart = true;
                     this.dispatchEvent(new PageEvent(PageEvent.READY_TO_START,false,false,0,this));
                }
                
                public override function startPage():void
                {
                     addListeners();
                     _video.seek(_startTime);
                }
                
                public override function resumePage():void
                {
                     addListeners();
                     
                     _video.play();
                }
                
                public override function pausePage():void
                {
                     removeListeners();
                     
                     _video.pause();
                }
                
                public override function stopPage():void
                {
                     removeListeners();
                     
                     var ar:Boolean = _video.autoRewind;
                     
                     _video.autoRewind = true;
                     _video.stop();
                     _video.autoRewind = ar;
                }
                
                public function seekPage(seekTo:Number):void
                {
                     if(seekSafe)
                     {
                          removeListeners();
                          _video.pause();
                          
                          _video.addEventListener(VideoEvent.PLAYHEAD_UPDATE,doSeekSafe);
                          _video.seek(seekTo);
                          seekSafe = false;
                     }
                }
                
                private function doSeekSafe():void
                {
                     _video.removeEventListener(VideoEvent.PLAYHEAD_UPDATE,doSeekSafe);
                     
                     addListeners();
                     
                     _video.dispatchEvent(new VideoEvent(VideoEvent.PLAYHEAD_UPDATE));
                     
                     seekSafe = true;
                }
                
                private function addListeners():void
                {
                     _video.addEventListener(VideoEvent.COMPLETE,dispatchPageComplete);
                     _video.addEventListener(VideoEvent.PLAYHEAD_UPDATE,doVidProgress);
                }
                
                private function removeListeners():void
                {
                     _video.removeEventListener(VideoEvent.COMPLETE,dispatchPageComplete);
                     _video.removeEventListener(VideoEvent.PLAYHEAD_UPDATE,doVidProgress);
                }
                
                private function doVidProgress(e:VideoEvent):void
                {
                     var scrubObj:Object = new Object();
                     scrubObj.currentTime = (e.currentTarget as VideoPlayer).playheadTime;
                     scrubObj.totalTime = _endTime - _startTime;
                     
                     trace("playheadTime | "+e.currentTarget.playheadTime);
                     trace("end time | "+_endTime);
                     
                     dispatchUpdateScrubber(scrubObj);
                     
                     checkForCuePoint(e);
                     
                     if(_video.playheadTime >= _endTime)
                     {
                          dispatchPageComplete();
                     }
                }
                
                private function checkForCuePoint(e:VideoEvent):void
                {
                     
                     if(_cuePoints.length > 0)
                     {
                          if( (e.currentTarget as VideoPlayer).playheadTime > _cuePoints[0].toString() && !resetting )
                          {
                               var item:Object = new Object();
                               item.point = _pageXML..CuePoints[_displayedPoints.length].toString()+"<br><br>";
                               
                               dispatchUpdateScrubber(item);
                               
                               _displayedPoints.push(_cuePoints.shift());                         
                          }
                     }
                     
                }
           }
      }