Hey guys, I have a problem thats got me baffled.
I’ve made a video class that holds a video. I want to be able to create an instance of this class, give it a video to preload, check if its all preloaded, then start playing it.
Currently the only way i can find to start it preloading is to call ‘ns.play()’, however this starts the video actually playing.
Is it netstream that i should actually be using here?
Thanks
package{
import flash.display.Sprite;
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.media.Video;
import flash.events.*;
import flash.utils.*;
public class Video1 extends Holder {
private var vid:Video;
private var ns:NetStream;
private var url:String;
private var theHeight:int;
private var theWidth:int;
private var duration:Number;
private var playTimer:Timer;
public function Video1( theData, theSizes ) {
url = theData.attributes.url;
transition = theData.attributes.transition;
super( theSizes, 'video', transition );
loaded = false;
vid = new Video();
vid.smoothing = true;
var nc:NetConnection = new NetConnection();
nc.connect(null);
ns = new NetStream(nc);
var netClient:Object = new Object();
netClient.onMetaData = onMetaData;
ns.client = netClient;
vid.attachNetStream(ns);
ns.addEventListener(NetStatusEvent.NET_STATUS, netstat);
playTimer = new Timer( 4000 ); //4 second delay before actually playing video
playTimer.addEventListener("timer", playEvent);
addChild(vid);
}
//start playing video
private function playEvent(eventArgs:TimerEvent){
playTimer.stop();
ns.play();
}
//start preloading video
override function preload():void {
ns.play(url);
}
//size video, start play
override function go():void {
playTimer.start();
var xscale = sizes.wide / vid.width;
var yscale = sizes.tall / vid.height;
//scale by smallest amount
if( xscale < yscale ){
//landscape
vid.scaleX = xscale;
vid.scaleY = xscale;
vid.y = (sizes.tall - vid.height) / 2;
}else{
//portrait
vid.scaleX = yscale;
vid.scaleY = yscale;
vid.x = (sizes.wide - vid.width) / 2;
}
}
override function getDuration():Number{
return Math.ceil(duration * 1000) + 4000;
}
public function onMetaData(info:Object):void {
duration = info.duration;
ns.bufferTime = duration;
}
function netstat(stats:NetStatusEvent) {
if( stats.info.code == 'NetStream.Buffer.Full' ){
loaded = true;
}
}
}
}
If you made it this far, i admire you :proud: