Skip to main content
Apr 20, 2010 7:01 PM UTC
Answered

Can't pause my stream

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 is closed to new replies. Start a new post to keep the conversation going.
    Correct answer
    Apr 21, 2010 1:10 AM UTC

    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;
         }
    }

    2 replies

    Correct answer
    Apr 21, 2010 1:10 AM UTC

    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;
         }
    }
    Translate
    Apr 21, 2010 12:57 PM UTC

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

    Thanks

    Translate