Skip to main content
April 20, 2010
Answered

Can't pause my stream

  • April 20, 2010
  • 1 reply
  • 565 views

Hi!

I've modified and created a display for a stream that starts automatically. I used "ns.play(sample)" to start the stream, but creating a button and calling the ns.pause() doesn't work. What's wrong?

var video:Video = new Video(550,310);
addChild(video);
var nc:NetConnection = new NetConnection();
nc.connect("rtmp://myserver/vod");
var meta:Object = new Object();
nc.addEventListener(NetStatusEvent.NET_STATUS, onStatusEvent);
function onStatusEvent(stat:Object):void {
     trace(stat.info.code);
     switch (stat.info.code) {
          case ("NetConnection.Connect.Success") :
               var ns:NetStream = new NetStream(nc);
               ns.client = meta;
               video.attachNetStream(ns);
               ns.play("sample");
               trace("Trying");
               break;
     }
}
meta.onMetaData =  metadataHandler;
function metadataHandler(metadataObj:Object):void {
     trace("Clip duration:  "+metadataObj.duration);
}
nc.addEventListener(AsyncErrorEvent.ASYNC_ERROR, dummyFunc);
function dummyFunc(dummy:AsyncErrorEvent){
          trace("AsyncError dummyevent fired: "+dummy);
}
pause_btn.addEventListener(MouseEvent.CLICK, pauseIt);
function pauseIt(info:MouseEvent):void{
     ns.pause(); // Why isn't this working???
}

Thanks for any help.

G

    This topic has been closed for replies.
    Correct answer

    Looks like a scope problem.

    You're defining the ns variable inside of a function (onStatusEvent) so as soon as the function is done executing, the reference to the variable is lost.

    Define the ns variable outside the function so the reference is in the scope of the class.

    var ns:NetStream;
    var nc:NetConnection = new NetConnection();
    nc.connect("rtmp://myserver/vod");
    var meta:Object = new Object();
    nc.addEventListener(NetStatusEvent.NET_STATUS, onStatusEvent);
    function onStatusEvent(stat:Object):void {
         trace(stat.info.code);
         switch (stat.info.code) {
              case ("NetConnection.Connect.Success") :
                   ns = new NetStream(nc);
                   ns.client = meta;
                   video.attachNetStream(ns);
                   ns.play("sample");
                   trace("Trying");
                   break;
         }
    }

    1 reply

    Correct answer
    April 21, 2010

    Looks like a scope problem.

    You're defining the ns variable inside of a function (onStatusEvent) so as soon as the function is done executing, the reference to the variable is lost.

    Define the ns variable outside the function so the reference is in the scope of the class.

    var ns:NetStream;
    var nc:NetConnection = new NetConnection();
    nc.connect("rtmp://myserver/vod");
    var meta:Object = new Object();
    nc.addEventListener(NetStatusEvent.NET_STATUS, onStatusEvent);
    function onStatusEvent(stat:Object):void {
         trace(stat.info.code);
         switch (stat.info.code) {
              case ("NetConnection.Connect.Success") :
                   ns = new NetStream(nc);
                   ns.client = meta;
                   video.attachNetStream(ns);
                   ns.play("sample");
                   trace("Trying");
                   break;
         }
    }
    April 21, 2010

    Ahh, thanks for that, Jay I had my suspicion but you nailed it.

    Thanks