Question

I have 3 flv videos that I'm streaming. The first one is the intro. Then the second one is playing after the intro right after the intro ends. And the second video is looping. Everything seemed to be fine. But some times when i load the swf it starts from the second video. Any ideas why ?

import flash.events.MouseEvent
var nc:NetConnection = new NetConnection();
nc.connect(null);

var ns:NetStream = new NetStream(nc);
loader.vid.Video_1.attachNetStream(ns);


var listener:Object = new Object();
listener.onMetaData = function(evt:Object):void {};
ns.client = listener;

ns.play("video_1.flv");

ns.addEventListener(NetStatusEvent.NET_STATUS, NCListener);
function NCListener(e:NetStatusEvent){
if (e.info.code == "NetStream.Buffer.Empty") {
ns.play("video_2.flv");
}
};

loader.button_01.addEventListener(MouseEvent.CLICK, play_video_01);

loader.button_01.addEventListener(MouseEvent.ROLL_OVER, play_effect_01);

function play_video_01 (event:Event):void{

    ns.play("video_3.flv");
    loader.button_01_mc.gotoAndPlay (21);

}
function play_effect_01 (event:Event):void{

    loader.button_01_mc.gotoAndPlay (2);
}
Was it helpful?

Solution

In your code, playback of your second video is triggered by the message NetStream.Buffer.Empty. That message can get dispatched for several reasons, as well as when playback of a video ends. For example when streaming (which I know you're not doing), NetStream.Buffer.Empty can get dispatched when there is a network problem. This is definitely the cause of your problem, but it's not clear why sometimes the buffer empty message gets dispatched right away.

The first thing I would do is modify your NetStatusEvent listener so that it traces out all of the messages that are being dispatched. That way you can see the sequence of events that occurs when this problem happens.

And second, you should try using another message to trigger playback of the second video. I'm not 100% sure, but I think the message NetStream.Play.Stop is what you want (this gets dispatched when the end of the video is reached, as well as when you programmatically stop playback). The full list of messages you get from a NetStatusEvent is here.

Incorporating both of these suggestions, your NetStatusEvent handler might look like this:

function NCListener(e:NetStatusEvent)
{
    var code:String = e.info.code;
    trace("code: ", code);
    if (code == "NetStream.Record.Stop"
        ns.play("video_2.flv");
}

Finally, you might want to add other event listeners to the NetStream. It dispatches an IOErrorEvent and AsyncErrorEvent ... perhaps you're getting one of these when the problem happens.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top