Hi all,

I am working on several projects that have very large videos (1-2 hours)
that need to be delivered on CD-Rom & Intranet. After trying to use one
large video file I realized there is a large problem when seeking to any
point in the video in that It has to buffer itself up to the point you
wish to play & on these large videos that can take quite some time.

This led me to chop the video up into 2 min chunks & write a segmented
video player so they play as one video. This is working great on most
machines but on the occasional Windows 98 machine it is crashing when
initially grabbing the flv's. 

I think the problem is that when initializing the component it is trying
to load all the flv's at once. 

Does anyone have any insight into this? Is there another segmented video
player about?

Here is the class:

/***************************************
* @author - James Tann
* 
* FILE                  :: SegmentedFLVPlayback.as
* PACKAGE               :: com.codesortium.video.SegmentedFLVPlayback
* AUTHOR                :: James Tann
* AUTHOR SITE   :: http://www.avtgroup.com
* CREATED               :: 10/04/2006
* DESCRIPTION   :: 
****************************************/
// ** AUTO-UI IMPORT STATEMENTS **
// ** END AUTO-UI IMPORT STATEMENTS **
import mx.video.FLVPlayback;
import mx.utils.Delegate;
import mx.events.EventDispatcher;

class com.codesortium.video.SegmentedFLVPlayback extends MovieClip {
// Constants:
        public static var CLASS_REF             :Function       =
com.codesortium.video.SegmentedFLVPlayback;
        public static var LINKAGE_ID    :String         =
"SegmentedFLVPlayback";
        public static var SymbolLinked  :Boolean        =
Object.registerClass(LINKAGE_ID, CLASS_REF);
        
// Public Properties:
        public var addEventListener                     :Function;
        public var removeEventListener          :Function;
        public var autoPlay                                     :Boolean
= false;
        public var bufferTime                           :Number
= 0.1;
        public var playheadTime                         :Number
= 0;
        public var playheadUpdateInterval       :Number         = 250;
        public var progressInterval                     :Number
= 250;
        public var state                                        :String
= "";
        public var totalTime                            :Number
= 0;
        public var stateResponsive                      :Boolean
= false;
        public var buffering                            :Boolean
= false;
// Private Properties:
        private var dispatchEvent               :Function;
        private var _arrFLVPlayback             :Array;
        private var _activeFlvPlayback  :FLVPlayback;
        private var _totalLength                :Number
= 0;
        private var _position                   :Number
= 0;
        private var _volume                             :Number
= 100;
        private var _arrCuePoints               :Array;
// UI Elements:

// ** AUTO-UI ELEMENTS **
        private var mcBackground:MovieClip;
// ** END AUTO-UI ELEMENTS **

// Initialization:
        private function SegmentedFLVPlayback() {
                EventDispatcher.initialize(this);
                _arrFLVPlayback         = new Array();
                _arrCuePoints           = new Array();
        }
        private function onLoad():Void { 
                configUI(); 
        }

// Public Methods:
        public function toString() : String {
                return "com.codesortium.video.SegmentedFLVPlayback";
        }
        
        public function get width():Number{
                return _width;
        }
        
        public function set width(intSet:Number):Void{
                _width = intSet;
        }
        
        public function get height():Number{
                return _height;
        }
        
        public function set height(intSet:Number):Void{
                _height = intSet;
        }
        
        public function get length():Number{
                return _totalLength;
        }
        
        public function get playing():Boolean{
                return _activeFlvPlayback.playing;
        }
        
        public function get volume():Number{
                return _volume;
        }
        
        public function set volume(intSet:Number):Void{
                _volume = intSet;
                
                for(var i:Number=0; i<_arrFLVPlayback.length; i++){
                        FLVPlayback(_arrFLVPlayback[i]).volume =
_volume;
                }
        }
        
        public function addFLV(strURI:String):Void{
                var newFLVPlayback      :FLVPlayback;
                
                newFLVPlayback =
FLVPlayback(this.attachMovie("FLVPlayback","FLVPlayback" +
_arrFLVPlayback.length, this.getNextHighestDepth()));
                
                newFLVPlayback.addEventListener("metadataReceived",
Delegate.create(this, onMetadata));
                newFLVPlayback.addEventListener("ready",
Delegate.create(this, onReady));
                newFLVPlayback.addEventListener("playheadUpdate",
Delegate.create(this, onPlayheadUpdate));
                newFLVPlayback.addEventListener("stateChange",
Delegate.create(this, onStateChange));
                
                newFLVPlayback.addEventListener("complete",
Delegate.create(this, onComplete));
                
                newFLVPlayback.maintainAspectRatio              = false;
                newFLVPlayback.autoPlay
= false;
                newFLVPlayback.bufferTime
= bufferTime;
                newFLVPlayback.playheadUpdateInterval   =
playheadUpdateInterval;
                newFLVPlayback.progressInterval                 =
progressInterval;
                
                if(_arrFLVPlayback.length != 0){
                        newFLVPlayback._visible = false;
                }else{
                        _activeFlvPlayback                      =
newFLVPlayback;
                        _activeFlvPlayback.autoPlay     = autoPlay;
                }
                
                newFLVPlayback.contentPath
= strURI;
                _arrFLVPlayback.push(newFLVPlayback);
        }
        
        public function pause():Void{
                _activeFlvPlayback.pause();
        }
        
        public function play():Void{
                _activeFlvPlayback.play();
        }
        
        public function stop():Void{
                _activeFlvPlayback.stop();
                seek(0);
        }
        
        public function seek(time:Number):Void{
                var blnSet              :Boolean        = false;
                var intLength   :Number         = 0;
                var blnPlaying  :Boolean        =
_activeFlvPlayback.playing;
                
                buffering       = true;
                dispatchEvent({type:"seeking", target:this});
                
                for(var i:Number=0; i<_arrFLVPlayback.length; i++){
                        if(!blnSet){
                                var flvCurrent  :FLVPlayback    =
_arrFLVPlayback[i];
                                
                                if(time < (intLength +
flvCurrent.totalTime)){
                                        if(flvCurrent !=
_activeFlvPlayback){
        
_activeFlvPlayback.stop();
                                                
                                                _activeFlvPlayback =
flvCurrent;
                                                
        
_activeFlvPlayback.seek(time - intLength);
                                                
                                                if(blnPlaying){
        
_activeFlvPlayback.play();
                                                }
                                        }else{
                                                flvCurrent.seek(time -
intLength);
                                        }
                                        
                                        blnSet = true;
                                }else{
                                        intLength +=
flvCurrent.totalTime;
                                }
                        }
                }

        }
        
        public function addCuePoint(intNew:Number):Void{
                var blnAdd      :Boolean        = true;
                
                for(var i:Number=0; i<_arrCuePoints.length; i++){
                        if(_arrCuePoints[i] == intNew){
                                blnAdd = false;
                        }
                }
                
                if(blnAdd){
                        _arrCuePoints.push(intNew);
                        _arrCuePoints.sort(Array.NUMERIC);
                }
        }
        
        public function removeCuePoint(intRemove:Number):Void{
                var blnAdd      :Boolean        = true;
                
                for(var i:Number=0; i<_arrCuePoints.length; i++){
                        if(_arrCuePoints[i] == intRemove){
                                _arrCuePoints.splice(i,1);
                                _arrCuePoints.sort(Array.NUMERIC);
                                break;
                        }
                }
        }
        
        public function seekToNextCuePoint():Void{
                for(var i:Number=0; i<_arrCuePoints.length; i++){
                        if((_arrCuePoints[i] < playheadTime)){
                                if(_arrCuePoints[i+1] != null){
                                        if(_arrCuePoints[i+1] >
playheadTime){
        
seek(_arrCuePoints[i+1]);
                                        }
                                }
                        }
                }
        }
        
        public function seekToLastCuePoint():Void{
                for(var i:Number=0; i<_arrCuePoints.length; i++){
                        if((_arrCuePoints[i] < playheadTime)){
                                if(_arrCuePoints[i+1] != null){
                                        if(_arrCuePoints[i+1] >
playheadTime){
                                                if(((_arrCuePoints[i] +
2) > playheadTime) && (_arrCuePoints[i-1] != null)){
        
seek(_arrCuePoints[i-1]);
                                                }else{
        
seek(_arrCuePoints[i]);
                                                }
                                        }
                                }
                        }
                }
        }
        
// Semi-Private Methods:
// Private Methods:
        private function configUI():Void {
        }
        
        private function seekPercent(time:Number):Void{
                seek((totalTime/100)*time);
        }

        private function onMetadata(event:Object,
delegate:Function):Void{
                totalTime += event.info.duration;
                _totalLength += (event.info.duration * 1000);
                dispatchEvent({type:"metadata", target:this});
                event.target.removeEventListener("metadata", delegate);
        }
        
        private function onReady(event:Object, delegate:Function):Void{
                FLVPlayback(event.target).volume = _volume;
                stateResponsive = true;
                dispatchEvent({type:"ready", target:this});
                event.target.removeEventListener("ready", delegate);
        }
        
        private function onPlayheadUpdate(event:Object,
delegate:Function):Void{
                var blnSet              :Boolean        = false;
                var intLength   :Number         = 0;
                
                for(var i:Number=0; i<_arrFLVPlayback.length; i++){
                        var flvCurrent          :FLVPlayback    =
_arrFLVPlayback[i];
                        var blnIsCurrent        :Boolean
= (flvCurrent == _activeFlvPlayback);
                        
                        flvCurrent._visible = blnIsCurrent;
                        
                        if(!blnSet){
                                if(blnIsCurrent){
                                        intLength       += playheadTime
+ (event.playheadTime - playheadTime);
                                        blnSet          = true;
                                }else{
                                        intLength       +=
flvCurrent.totalTime;
                                }
                        }
                }
                
                playheadTime = intLength;
                dispatchEvent({type:"playheadUpdate", target:this,
playheadTime:playheadTime});
        }
        
        private function onStateChange(event:Object,
delegate:Function):Void{
                if(event.target == _activeFlvPlayback){
                        var objEvent    :Object = new Object();
                        objEvent.target                 = this;
                        objEvent.type                   = "stateChange";
                        objEvent.state                  = event.state;
                        objEvent.playheadTime   = playheadTime;
                        
                        dispatchEvent(objEvent);
                        
                        switch(event.state){
                                case "buffering":
                                        buffering       = true;
                                        dispatchEvent({type:"buffering",
target:this});
                                break;
                                case "playing":
                                        buffering       = false;
                                        dispatchEvent({type:"playing",
target:this});
                                break;
                                case "stopped":
                                        buffering       = false;
                                        dispatchEvent({type:"stopped",
target:this});
                                break;
                                case "paused":
                                        buffering       = false;
                                        dispatchEvent({type:"paused",
target:this});
                                break;
                        }
                }
        }
        
        private function onComplete(event:Object,
delegate:Function):Void{
                var blnSet      :Boolean        = false;
                
                if(_arrFLVPlayback.length > 1){ 
                        for(var i:Number=0; i<_arrFLVPlayback.length;
i++){
                                if(!blnSet){
                                        if(_arrFLVPlayback[i] ==
_activeFlvPlayback){
                                                blnSet
= true;
        
_activeFlvPlayback._visible = false;
                                                
                                                if(i ==
(_arrFLVPlayback.length-1)){
        
_activeFlvPlayback = _arrFLVPlayback[0];
        
_activeFlvPlayback._visible = true;
        
dispatchEvent({type:"complete", target:this});
                                                }else{
        
_activeFlvPlayback = _arrFLVPlayback[i+1];
                                                        
        
_activeFlvPlayback._visible = true;
        
_activeFlvPlayback.play();
                                                }
                                        }
                                }
                        }
                }
        }
}


Cheers
Jim

_______________________________________________
osflash mailing list
[email protected]
http://osflash.org/mailman/listinfo/osflash_osflash.org

Reply via email to