PDA

View Full Version : Preloading a video without playing



gloversure
May 22nd, 2009, 11:24 AM
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:

ofeet
May 22nd, 2009, 11:54 AM
http://help.adobe.com/en_US/AS3LCR/Flash_10.0/fl/video/VideoPlayer.html

I've since switched to that. in order to import fl.video.VideoPlayer and associated classes, you need to have the FLVPlayback component in your library.

you can player.load() to just load the flv without playing it.
there's also player.playWhenEnoughDownloaded() which will make sure there's a good buffer before playing.

gl

gloversure
May 22nd, 2009, 12:02 PM
I did see that class, but assumed it was one of flashs components that added an on screen player or something.
I'll have a play with it and see how i get on. Thanks!

efos
May 22nd, 2009, 02:22 PM
you can player.load() to just load the flv without playing it.
there's also player.playWhenEnoughDownloaded() which will make sure there's a good buffer before playing.

gl

FLVPlayer has both of those methods?

FLVPlayer wraps a VideoPlayer.