Reputation: 21
my flash site needs to play let's say 10 videos one after each other, to reduce waiting time I need to preload video 2 and video 3 while video 1 is playing and so on...
I need to follow this structure because at the end of video 1 users will have the chance to choose which will be the next (2 or 3).
All the code is on the first frame of my movie, I still have to learn classes, packages etc.
The first video is played through a linked video (myVideo) that I placed on the stage with the following code:
var nc:NetConnection = new NetConnection();
nc.connect(null);
var ns:NetStream = new NetStream(nc);
ns.addEventListener(NetStatusEvent.NET_STATUS, statusHandler);
ns.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
ns.play("video1.f4v");
var myVideo:Video = new Video();
myVideo.attachNetStream(ns);
addChild(myVideo);
I need to understand the best way to preload the other two videos while nr. 1 is playing.
Shall I create 2 more linked videos, place them out of the stage and load videos there so they will be immediately available when needed?
Thanks very much for your help!
--- EDIT: Added more details to my post ---
I'm doing some test to understand this procedure, I've tried the following:
var ns2:NetStream = new NetStream(nc);
var ns3:NetStream = new NetStream(nc);
ns2.play("video/video2.f4v");
ns2.pause();
ns2.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
ns3.play("video/video3.f4v");
ns3.pause();
ns3.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
it works, but loading 2 movies in this way will result in 100% cpu usage and poor frame rate of video playing... maybe I have to load video 2, check for loading complete and then start loading video 3... is this the proper way to do that?
Thanks again
Upvotes: 1
Views: 2206
Reputation: 21
I believe I found the way, here is the full script:
var nc:NetConnection = new NetConnection();
nc.connect(null);
var ns:NetStream = new NetStream(nc);
ns.addEventListener(NetStatusEvent.NET_STATUS, statusHandler);
ns.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
ns.play("video1.f4v");
var myVideo:Video = new Video();
myVideo.attachNetStream(ns);
addChild(myVideo);
var ns2:NetStream = new NetStream(nc);
var ns3:NetStream = new NetStream(nc);
function statusHandler(event:NetStatusEvent):void
{
switch (event.info.code)
{
case "NetStream.Play.Start" :
ns2.play("video/video2.f4v");
ns2.pause();
ns2.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
ns3.play("video/video3.f4v");
ns3.pause();
ns3.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
break;
case "NetStream.Play.Stop" :
trace("ns video stop");
break;
}
}
when I call ns.play("video2.f4v"); the video is already in the browser cache so immediately starts playing and Cpu usage looks good too.
If there is a more professional way I'd like to learn more.
trace("thanks, see you soon");
Upvotes: 1
Reputation: 6742
The easiest way of achieving this would be using Greensock's LoaderMax. You can set up the list of videos you want to load and then prioritise some of them depending of user's actions.
LoaderMax has some great functionality which would be quite hard to build from scratch, it also fixes some weird Flashplayer behaviour.
There is a little bit of learning curve but it is not too steep.
Upvotes: 0