FORUMS: list search recent posts

How to preload an swf with video? (using netconnection)

COW Forums : Adobe Flash

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
dean chapman
How to preload an swf with video? (using netconnection)
on May 14, 2010 at 10:56:52 pm

I have a swf (labeled "Video_Playback.swf") with a simple video setup: flv to net connection to net stream to video. The only movie clips on the actual stage are the play and stop buttons. This swf works just fine on it's own. However, i simply cannot figure out how to link this "Video_Playback.swf" to another .fla file.......ideally, a preloader which loads it in...
If you can take this code (from the "Video_Playback.swf")
and make it communicate properly to any .fla, i would be sooo grateful.

oh ya, here's the error message i'm getting: "Error #2044: Unhandled NetStatusEvent:. level=error, code=NetStream.Play.StreamNotFound
at Video_Playback_FIN_fla::MainTimeline/frame1()"

This is the "Video_Playback.swf" code:

var videoConnection:NetConnection = new NetConnection();
videoConnection.connect(null);
var videoStream:NetStream = new NetStream(videoConnection);
videoStream.play("short_jump.flv");
var metaListener:Object = new Object();
metaListener.onMetaData = onMetaData;
videoStream.client = metaListener;
var video:Video = new Video();
video.attachNetStream(videoStream);
addChild(video);

function onMetaData(data:Object):void
{
play_btn.addEventListener(MouseEvent.CLICK, playMovie);
stop_btn.addEventListener(MouseEvent.CLICK, stopMovie);
}

function playMovie(event:MouseEvent):void
{
videoStream.play("short_jump.flv");
}

function stopMovie(event:MouseEvent):void
{
videoStream.pause();
}

//////
i've also included the code i'm using for my "Preloader.swf" which is having trouble loading the "Video_Playback.swf"
All of the "Video_Playback.fla" files (including the "Video_Playback.swf" & "short_jump.flv") are in a folder labeled "swfs". The only thing on the actual stage of the "Preloader.swf" is a dynamic text field with instance name of "percent"
Here's the code for my "Preloader.swf":

var l:Loader = new Loader();
l.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loop);
l.contentLoaderInfo.addEventListener(Event.COMPLETE, done);
l.load(new URLRequest("swfs/Video_Playback.swf"));

function loop(e:ProgressEvent):void
{
var perc:Number = e.bytesLoaded / e.bytesTotal;
percent.text = Math.ceil(perc*100).toString();
}

function done(e:Event):void
{
removeChildAt(0);
percent = null;
addChild(l);
}


Return to posts index

Pieter Helsen
Re: How to preload an swf with video? (using netconnection)
on May 15, 2010 at 12:57:35 pm

First of all, that error you are getting is caused by not having a NetStatusEvent listener attached to the netstream object. Your code should look something like this:


// create netconnection
var videoConnection:NetConnection = new NetConnection();
videoConnection.connect(null);

// create client - Cuepoint and XMPData are not necessary, but handy if you ever do decide to use them
var metaListener:Object = new Object();
metaListener.onMetaData = onMetaData;
metaListener.onCuePoint = onCuePoint;
metaListener.onXMPData = onXMPData;

// create netstream
var videoStream:NetStream = new NetStream(videoConnection);
videoStream.addEventListener(NetStatusEvent.NET_STATUS, statusHandler);
videoStream.client = metaListener;
videoStream.play("short_jump.flv");

// create video
var video:Video = new Video();
video.attachNetStream(videoStream);
addChild(video);

// status handler
function statusHandler(evt:NetStatusEvent):void {

switch(evt.info.code){

case "NetStream.Play.StreamNotFound":
trace("The file you tried to play was not found.");
break;
case "NetStream.Buffer.Empty":
trace("The buffer is empty, and playback will stop until enough of the buffer has been filled (preloaded) again");
break;
case "NetStream.Buffer.Full":
trace("Playback will resume");
break;
case "NetStream.Play.Stop":
trace("Playback has ended");
break;

default:
// for more on the info codes, look up http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/flash/events/...

}

}


I'm not entirely sure what you want the preloader to do, but if you want the preloader to preload the video file, then that's not going to happen :) Your preloader will just preload the swf containing the video file (which is probably only about 3kb, so it will load very quickly. Normally, when you use the NetStream object, the video will just start to play as soon as the buffer is full... You can set that buffer through the videoStream.bufferTime property.
So for instance if you set videoStream.bufferTime = 20; then your video will preload 20 seconds of video before actually starting to play the video. Now, setting the buffer to 20 doesn't mean that it will take 20 seconds to preload. Depending on the user's internet connection, it can take a mere 5 seconds to buffer that amount, or, if he's on a slow connection it can take up to 40 seconds or more. If you want to show a loader whenever the video is buffering, just listen to the netstatus event for the following codes: "NetStream.Buffer.Empty" (meaning that we still haven't filled our 20 second buffer) and "NetStream.Buffer.Full" (meaning that the buffer is full, and we can hide our preloader)

Now... to calculate a percentage for a buffer is quite simple. We just take the current buffer length and divide it by the bufferTime we set, and then multiply it by 100 to get the percentage. And we do this everytime we receive the "NetStream.Buffer.Empty" event code.

var perc:Number = Math.round(videoStream.bufferLength / videoStream.bufferTime * 100)

So that's for buffering. If for some reason, you want to preload the entire video before playing it, instead of just streaming it, then you could do that like this:


// we add a few lines to the original code at the top...
// create netstream
var videoStream:NetStream = new NetStream(videoConnection);
videoStream.addEventListener(NetStatusEvent.NET_STATUS, statusHandler);
videoStream.client = metaListener;
videoStream.play("short_jump.flv");
videoStream.pause(); // don't play the video immediately

// add enter frame handler
this.addEventListener(Event.ENTER_FRAME, frameHandler);
function frameHandler(evt:Event):void {
if(videoStream.bytesLoaded < videoStream.bytesTotal){
// file hasn't loaded completely... calculate percentage
var perc:Number = Math.round(videoStream.bytesLoaded / videoStream.bytesTotal * 100);
}else{
// file was loaded completely... so resume playback :)
videoStream.resume();
}
}


My advice to you would be to not use the Preloader.fla file, since it can just as easily be done in the playback.fla file. Also, I don't recommend preloading the entire file before playing it. Streaming is far better since a user can watch the video file almost immediately.

Kind regards,
Pieter

General notice: from now on, I would like to ask everyone to put [AS2] or [AS3] (corresponding to the version of actionscript you are using on your project) in front of their post titles when the question is actionscript related! Please help us help you faster. Thank you.


Return to posts index

dean chapman
Re: How to preload an swf with video? (using netconnection)
on May 16, 2010 at 1:46:22 am

Hi Pieter,

So, if i ditch the preloader.fla, how do i set this preloader up in the playback.fla?
I'm trying to build a portfolio site....
i was hoping to have a preloader on it's own .fla, which would load in a default main page (in this case the playback.swf, containing the netconnection, netstream, and video stuff) and also incorporate more buttons on the main page (playback.fla) which would load in other swfs containing different pages...this workflow seems to work fine with swfs containing galleries, graphics, and text...but like you said, the video does not come in. Just so that i'm clear on this, are you saying that you simply cannot load this playback.swf to a button, preloader, or movieclip of a separate .fla file?

thanks!

dean




Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2018 CreativeCOW.net All Rights Reserved
[TOP]