actionscript-3flashactionscriptnetstreamnetconnection

How to pause and restart a NetStream object?


I have a NetStream object, based upon the following code:

streamID = "mystreamkey";
videoURL = "rtmp://mystreamurl";

vid = new Video();

nc = new NetConnection();

nc.addEventListener(NetStatusEvent.NET_STATUS, onConnectionStatus);
nc.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
nc.client = { onBWDone: function():void{}, streamConnected: streamConnected, streamDisconnected: streamDisconnected };
nc.connect(videoURL);

...

metaListener = new Object();
metaListener.onMetaData = received_Meta;
netStreamObj.client = metaListener;

netStreamObj.play(streamID);
vid.attachNetStream(netStreamObj);
addChild(vid);

All this is working fine, and I can also use netStreamObj.pause() to pause the live stream.

But how do I restart it? netStreamObj.play() won't work - it requires one parameter, so then I wrote this:

netStreamObj.play(streamID);
vid.attachNetStream(netStreamObj);

But this isn't working either.

How do I restart the stream, so that it connects to the live stream it was previously connected to?


Solution

  • How do I restart the stream, so that it connects to the live stream it was previously connected to?

    For future reference, check the NetStream API documentation for options. You'll be looking to check out the commands listed under the Public methods section.

    You can use either :

    resume(): Resumes playback of a video stream that is paused.

    usage : netStreamObj.resume();

    togglePause(): Pauses or resumes playback of a stream.

    usage : netStreamObj.togglePause();


    I suggest using togglePausesince it auto-detects the playback state of the NetStream object. Your "Pause" button (or MovieClip) should simply have a click listener event whose function has this line : netStreamObj.togglePause();. It's that easy...