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;
}
}