AS3 Scrubber bar problem

Hello, I am new to AS3 and I am a bit confused. I am creating a custom scrubber bar for my .flv. It is working, but its not working very well. When I drag the scrubber across the track it works ok but it flickers, then when I release it then it jumps to a different places and will not seek very precisely.

Thanks for any help
var video:Video = new Video(465.348);
video_mc.addChild(video);
 
var nc:NetConnection = new NetConnection();
nc.connect(null);
 
var ns:NetStream = new NetStream(nc);
ns.addEventListener(NetStatusEvent.NET_STATUS, onStatusEvent);
 
function onStatusEvent(stat:Object):void
{
	trace(stat.info.code);
}
var metaListener:Object = new Object();
metaListener.onMetaData = theMeta;
ns.client = metaListener;
video.attachNetStream(ns);
 
ns.play("theVideo.flv");
scrubber.scrubHandle.buttonMode = true;
stage.addEventListener(Event.ENTER_FRAME, enterFrame);
scrubber.scrubHandle.addEventListener(MouseEvent.MOUSE_DOWN, scrubDown);
scrubber.scrubHandle.addEventListener(MouseEvent.MOUSE_UP, scrubTo);
 
var xOffset:Number;
var xMin:Number = 0;
var xMax:Number = 430;
var totalLength:uint;
 
function theMeta(data:Object):void
{
	totalLength = data.duration;
}
 
 
function enterFrame(e:Event):void
	{
	var currentSecs:Number = Math.floor(ns.time);	
	var totalSecs:Number = Math.round(totalLength);
	if(currentSecs > 0)
	{
	var amountPlayed:Number = ns.time / totalLength;
	var amountLoaded:Number = ns.bytesLoaded / ns.bytesTotal;
	scrubber.track.width = amountLoaded * 430;
	scrubber.scrubHandle.x = ns.time / totalLength * 430;
 }
}
 
function scrubDown(e:MouseEvent):void
{
	stage.addEventListener(MouseEvent.MOUSE_MOVE, scrubFollow);
	xOffset = mouseX - scrubber.scrubHandle.x;
}
 
function scrubFollow(e:MouseEvent):void
{
	scrubber.scrubHandle.x = mouseX - xOffset
	if(scrubber.scrubHandle.x <= xMin)
	scrubber.scrubHandle.x = xMin;
	if(scrubber.scrubHandle.x >= xMax)
	scrubber.scrubHandle.x = xMax;
	e.updateAfterEvent();
	}
	
function scrubTo(e:MouseEvent):void
{
	stage.removeEventListener(MouseEvent.MOUSE_MOVE, scrubFollow);
	stage.removeEventListener(MouseEvent.MOUSE_MOVE, scrubDown);
if(scrubber.scrubHandle.mouseX < scrubber.track.width)
var percentAcross:Number = (scrubber.scrubHandle.x) / scrubber.track.width;
	ns.seek(totalLength * percentAcross);
 
e.updateAfterEvent();
}

Open in new window

LVL 2
jordanaustinAsked:
Who is Participating?
 
jordanaustinConnect With a Mentor Author Commented:
Ok, I solved it myself. In case anyone else is wondering I've posted the code below. Also if anyone see's a better way to do this please let me know. Thanks!
var video:Video = new Video(465.348);
video_mc.addChild(video);
 
var nc:NetConnection = new NetConnection();
nc.connect(null);
 
var ns:NetStream = new NetStream(nc);
ns.addEventListener(NetStatusEvent.NET_STATUS, onStatusEvent);
 
function onStatusEvent(stat:Object):void
{
	trace(stat.info.code);
}
var metaListener:Object = new Object();
metaListener.onMetaData = theMeta;
ns.client = metaListener;
video.attachNetStream(ns);
 
ns.play("theVideo.flv");
scrubber.scrubHandle.buttonMode = true;
stage.addEventListener(Event.ENTER_FRAME, enterFrame);
scrubber.scrubHandle.addEventListener(MouseEvent.MOUSE_DOWN, scrubDown);
scrubber.scrubHandle.addEventListener(MouseEvent.MOUSE_UP, scrubTo);
 
var xOffset:Number;
var xMin:Number = 0;
var xMax:Number = 430;
var totalLength:uint;
 
function theMeta(data:Object):void
{
	totalLength = data.duration;
}
 
 
function enterFrame(e:Event):void
	{
	var currentSecs:Number = Math.floor(ns.time);	
	var totalSecs:Number = Math.round(totalLength);
	if(currentSecs > 0)
	{
	var amountPlayed:Number = ns.time / totalLength;
	var amountLoaded:Number = ns.bytesLoaded / ns.bytesTotal;
	scrubber.track.width = amountLoaded * 430;
	scrubber.scrubHandle.x = ns.time / totalLength * 430;
 }
}
 
function scrubDown(e:MouseEvent):void
{
	stage.addEventListener(MouseEvent.MOUSE_MOVE, scrubFollow);
	stage.removeEventListener(Event.ENTER_FRAME, enterFrame);
	xOffset = mouseX - scrubber.scrubHandle.x;
}
 
function scrubFollow(e:MouseEvent):void
{
	scrubber.scrubHandle.x = mouseX - xOffset
	if(scrubber.scrubHandle.x <= xMin)
	scrubber.scrubHandle.x = xMin;
	if(scrubber.scrubHandle.x >= xMax)
	scrubber.scrubHandle.x = xMax;
	e.updateAfterEvent();
	}
	
function scrubTo(e:MouseEvent):void
{
	stage.removeEventListener(MouseEvent.MOUSE_MOVE, scrubFollow);
	var percentAcross:Number = (scrubber.scrubHandle.x) / scrubber.track.width;
	ns.seek(totalLength * percentAcross);
	stage.removeEventListener(MouseEvent.MOUSE_MOVE, scrubDown);
		stage.addEventListener(Event.ENTER_FRAME, enterFrame);
 
 e.updateAfterEvent();
}

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.