# Help custom video player class

**URL:** <https://forum.kirupa.com/t/help-custom-video-player-class/214431>\
**Category:** flash\
**Created:** [January 29, 2007, 12:40am UTC](https://forum.kirupa.com/t/help-custom-video-player-class/214431 "2007-01-29T00:40:23Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![esper](https://avatars.discourse-cdn.com/v4/letter/e/e9bcb4/32.png) [@esper](https://forum.kirupa.com/u/esper)\
**Post date:** [January 29, 2007, 12:40am UTC](https://forum.kirupa.com/t/help-custom-video-player-class/214431/1 "2007-01-29T00:40:23Z")

</div>

I read through the tutorials for Paul Newmans vid player. I am trying to make a custom video player class with the code below. Since I am new to custom classes, I am having some difficulties. When I use the code below on the timeline, everything works perfectly. The following Class code displays the video and sound. The mute button works. But the play and pause buttons trace out but do not function. Also, the scrub bar and load progress bar dont work.

I have been at this for hours! Any help would be greatly appreciated!!!

=================Timeline Code=======================  
/\*  
var scrubbing:Boolean;  
var t:Number; // current playhead time (in seconds)  
var totalTime:Number; // length of FLV (in seconds)

var playing:Boolean = false;

var scrubberLength:Number = mcContainer.movieScrubber.clipTimeline.\_width;  
var scrubRange:Number = scrubberLength - mcContainer.movieScrubber.scrubDot.\_width;

// NetConnection object  
var video\_nc:NetConnection;  
// NetStream object  
var video\_ns:NetStream  
// Sound object  
var oSound:Sound;  
var soundLevel:Number;

var intID:Number;

// display FLV download progress  
function updateProgress():Void{  
var bl:Number = video\_ns.bytesLoaded;  
var bt:Number = video\_ns.bytesTotal;  
mcContainer.movieScrubber.loadProgress.\_width = Math.round(bl/bt \* mcContainer.movieScrubber.clipTimeline.\_width);

if(bl \> 4 && bt \> 4 && bl \>= bt){  
//trace(“FLV loaded”);  
clearInterval(intID);  
intID = null;  
}  
}

video\_onStatus = function(infobject):Void{  
for(var i in info){  
//trace("onStatus: " + i + " = " + info);  
}

if(info.code == “NetStream.Play.Stop”){  
// video is over  
videoCompleted();  
}  
}

video\_onMetaData = function(infobject):Void{  
for(var i in info){  
//trace("onMetaData: " + i + " = " + info);  
}

totalTime = info.duration;  
video\_ns.play(video\_ns.autoPlay ? false : true);  
playing = video\_ns.autoPlay;  
}

// --------------------------------------------------  
// button handlers  
// --------------------------------------------------

mcContainer.mcPlay.onRelease = function():Void{  
video\_ns.pause();  
playing = false;  
}

mcContainer.mcPause.onRelease = function():Void{  
video\_ns.pause();  
playing = true;  
}  
mcContainer.mcDownload.onRelease = function():Void {  
getURL(“videos/test.zip”);  
trace(“download hit”);  
}

mcContainer.mcCloser.onRelease = function():Void {

trace(“closer hit”);  
}

// --------------------------------------------------  
// Video scrubber  
// --------------------------------------------------

mcContainer.movieScrubber.scrubDot.onPress = function():Void{  
this.startDrag(false, 0, 0, scrubRange, 0);  
scrubbing = true;  
playing = false;  
// mute volume while scrubbing  
oSound.setVolume(100);  
}

mcContainer.movieScrubber.scrubDot.onRelease = function():Void{  
this.stopDrag();  
scrubbing = false;  
playing = true;  
video\_ns.pause(false);  
// restore volume  
setVolume(soundLevel);  
}

mcContainer.movieScrubber.onEnterFrame = function():Void{

if(scrubbing){  
t = (this.scrubDot.\_x/scrubberLength) \* totalTime;

// this pauses video until scrubber is released  
video\_ns.pause(true);  
video\_ns.seek(t);  
}  
else{  
// NetStream.time method returns current playhead time  
var scrubFactor:Number = video\_ns.time/totalTime;  
this.scrubDot.\_x = scrubRange \* scrubFactor;  
}  
}

// --------------------------------------------------  
// Volume controls  
// --------------------------------------------------

mcContainer.mcMute.onRelease = function() {  
var globalVolume:Sound = new Sound();  
globalVolume.setVolume((globalVolume.getVolume()+100) % 200);  
};

// --------------------------------------------------  
// Functions  
// --------------------------------------------------

function loadPreferences():Void{

setupVideo();

}

function setupVideo():Void{  
// set volume properties  
oSound = new Sound(this);  
//soundLevel = volume;  
setVolume(soundLevel);

// create a NetConnection object that can be used with a  
// NetStream object to play back external video (FLV) files  
video\_nc = new NetConnection();  
// open local connection  
video\_nc.connect(null);  
// create a NetStream object to play FLV files  
// through the specified NetConnection object  
video\_ns = new NetStream(video\_nc);  
// specifies NetStream instance to be displayed within  
// the boundaries of the Video object on the Stage  
mcContainer.videoPlayer.attachVideo(video\_ns);

// start playback of external video (FLV) file  
video\_ns.play(“videos/test.flv”);  
// update FLV download progress bar  
intID = setInterval(updateProgress, 100);  
// this is a workaround: define temp value for  
// totalTime variable until onMetaData is called  
totalTime = 1000;

// set event handlers  
video\_ns.onStatus = video\_onStatus;  
video\_ns.onMetaData = video\_onMetaData;  
}

function setVolume(v:Number):Void{

oSound.setVolume(100);

}

function videoCompleted():Void{  
// if video has ended, go to beginning  
video\_ns.seek(0);  
video\_ns.pause(loop ? false : true);  
playing = loop;  
}

loadPreferences();\*/

============Class Code=====================

import mx.events.EventDispatcher;

class TMNTVideoPlayer extends MovieClip {

private var scrubbing:Boolean;  
private var t:Number; // current playhead time (in seconds)  
private var totalTime:Number; // length of FLV (in seconds)

private var playing:Boolean = false;  
//private var autoPlay:Boolean;

private var movieScrubber:MovieClip;  
private var playpauseClip:MovieClip;  
private var mcDownload:MovieClip;  
private var mcCloser:MovieClip;  
private var mcMute:MovieClip;  
private var videoPlayer:MovieClip;  
private var mcPlay:MovieClip;  
private var mcPause:MovieClip;

var scrubberLength:Number;  
var scrubRange:Number;

// NetConnection object  
private var video\_nc:NetConnection;  
// NetStream object  
private var video\_ns:NetStream;  
// Sound object  
private var oSound:Sound;  
private var soundLevel:Number;

private var intID:Number;  
private var loadInterval:Number;

public var addEventListener:Function;  
public var removeEventListener:Function;  
public var dispatchEvent:Function;

//==============================================================================  
function TMNTVideoPlayer() {  
//==============================================================================  
EventDispatcher.initialize(this);

setMouseEvents();  
setScrubberEvents();  
setMute();  
setupVideo();  
updateProgress();  
loadPreferences();

trace(“constructor initialized”);  
};

// display FLV download progress  
//==============================================================================  
function updateProgress():Void {  
//==============================================================================  
trace(“updateProgress”);

scrubberLength = this.movieScrubber.clipTimeline.\_width;  
scrubRange = scrubberLength - this.movieScrubber.scrubDot.\_width;

var bl:Number = video\_ns.bytesLoaded;  
var bt:Number = video\_ns.bytesTotal;  
this.movieScrubber.loadProgress.\_width = Math.round(bl/bt \* this.movieScrubber.clipTimeline.\_width);

if(bl \> 4 && bt \> 4 && bl \>= bt){  
trace(“FLV loaded”);  
clearInterval(intID);  
intID = null;  
}  
}

//==============================================================================  
function video\_onStatus(infobject):Void {  
//==============================================================================  
trace(“onstatus”);  
for(var i in info){  
trace("onStatus: " + i + " = " + info);  
}

if(info.code == “NetStream.Play.Stop”){

videoCompleted();  
}  
}

//==============================================================================  
function video\_onMetaData(infobject):Void {  
//==============================================================================  
trace(“onMetaData”);  
for(var i in info){  
trace("onMetaData: " + i + " = " + info);  
}

totalTime = info.duration;  
this.video\_ns.play(this.video\_ns.autoPlay ? false : true);  
playing = this.video\_ns.autoPlay;  
}

//==============================================================================  
function setMouseEvents () {  
//==============================================================================  
trace(“setMouseEvents Working”);

this.mcPause.onRelease = function():Void {  
video\_ns.pause();  
playing = false;  
trace(“pause hit”);  
}

this.mcPlay.onRelease = function():Void {  
video\_ns.pause();  
playing = true;  
trace(“play hit”);  
}  
this.mcDownload.onRelease = function():Void {  
getURL(“videos/test.zip”);  
trace(“download hit”);  
}

this.mcCloser.onRelease = function():Void {

trace(“closer hit”);  
}

}  
//==============================================================================  
function setScrubberEvents() {  
//==============================================================================

trace(“Scrubberevents traced”);  
var scrubberLength:Number = this.movieScrubber.clipTimeline.\_width;  
var scrubRange:Number = scrubberLength - this.movieScrubber.scrubDot.\_width;

this.movieScrubber.scrubDot.onPress = function():Void{  
this.movieScrubber.scrubDot.startDrag(false, 0, 0, scrubRange, 0);  
scrubbing = true;  
playing = false;  
// mute volume while scrubbing  
oSound.setVolume(100);  
}

this.movieScrubber.scrubDot.onRelease = function():Void{  
this.movieScrubber.scrubDot.stopDrag();  
scrubbing = false;  
playing = true;  
this.video\_ns.pause(false);  
// restore volume  
//setVolume(soundLevel);  
}

this.movieScrubber.onEnterFrame = function():Void{

if(scrubbing){  
t = (this.movieScrubber.scrubDot.\_x/scrubberLength) \* totalTime;

// this pauses video until scrubber is released  
this.video\_ns.pause(true);  
this.video\_ns.seek(t);  
}  
else{  
// NetStream.time method returns current playhead time  
var scrubFactor:Number = this.video\_ns.time/totalTime;  
this.movieScrubber.scrubDot.\_x = scrubRange \* scrubFactor;  
}  
}  
}

//==============================================================================  
function setMute() {  
//==============================================================================

this.mcMute.onRelease = function() {  
var globalVolume:Sound = new Sound();  
globalVolume.setVolume((globalVolume.getVolume()+100) % 200);  
}

}

//==============================================================================  
function loadPreferences():Void {  
//==============================================================================  
setupVideo();

}

//==============================================================================  
function setupVideo():Void {  
//==============================================================================  
// set volume properties  
oSound = new Sound(this);  
//soundLevel = volume;  
//setVolume(soundLevel);

// create a NetConnection object that can be used with a  
// NetStream object to play back external video (FLV) files  
video\_nc = new NetConnection();  
// open local connection  
video\_nc.connect(null);  
// create a NetStream object to play FLV files  
// through the specified NetConnection object  
video\_ns = new NetStream(video\_nc);  
// specifies NetStream instance to be displayed within  
// the boundaries of the Video object on the Stage  
this.videoPlayer.attachVideo(video\_ns);

// start playback of external video (FLV) file  
video\_ns.play(“videos/test.flv”);  
// update FLV download progress bar  
//intID = setInterval(updateProgress, 100);  
// this is a workaround: define temp value for  
// totalTime variable until onMetaData is called  
totalTime = 1000;

// set event handlers  
video\_ns.onStatus = video\_onStatus;  
video\_ns.onMetaData = video\_onMetaData;  
}

//==============================================================================  
function setVolume(v:Number):Void{  
//==============================================================================  
oSound.setVolume(100);

}

//==============================================================================  
function videoCompleted():Void{  
//==============================================================================  
// if video has ended, go to beginning  
var loop:Boolean;  
this.video\_ns.seek(0);  
video\_ns.pause(loop ? false : true);  
playing = loop;  
}

}
