4 Replies Latest reply on Feb 5, 2007 12:21 PM by JaxonRice

    Beginner Question: controlling AS3 with Flex button

    JaxonRice
      Hi

      I am learning Flex and AS3 and was hoping someone here could tell me what I am doing wrong. I have a basic fLex application that plays an flv. For the purposes of my application I do not want to use a videoDisplay Flex component, so I am using an AS3 class to play the flv. Here is my flex application:

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">
      <mx:Script>
      <![CDATA[
      import flash.display.Sprite;

      public var _video:Sprite = new simpleFlv();

      public function init():void
      {
      rawChildren.addChildAt(_video,2);

      }
      public function playPause():void
      {
      ns.pause();
      }
      ]]>
      </mx:Script>
      <mx:Button label="Pause" click="playPause()" right="10" bottom="10"/>

      </mx:Application>

      and here is my simpleFlv class:

      package
      {
      import flash.display.Sprite;
      import flash.net.NetConnection;
      import flash.media.Video;
      import flash.net.NetStream;
      import flash.events.NetStatusEvent;
      import flash.display.Stage;

      public class simpleFlv extends Sprite
      {
      public function simpleFlv(){
      var nc:NetConnection= new NetConnection();
      nc.connect(null);
      var ns:NetStream = new NetStream(nc);
      var vid:Video = new Video(320,240);
      this.addChild(vid);
      vid.attachNetStream(ns);
      vid.smoothing=true;
      ns.play("foo.flv");
      ns.addEventListener(NetStatusEvent.NET_STATUS,netstat);
      function netstat(stats:NetStatusEvent):void
      {
      var netClient:Object = new Object();
      netClient.onMetaData = function(meta:Object):void
      {
      trace(meta.duration);
      };

      ns.client = netClient;
      }
      }
      }
      }

      The video plays when I run the application without including the pause button, but I get the following error when I try to run the application as coded above:

      1120: Access of undefined property ns

      I know I am missing something totally obvious here. How would I code a flex button to tell the flv to pause when I press it?

      Thanks in advance
        • 1. Re: Beginner Question: controlling AS3 with Flex button
          chris.huston.t10 Level 3
          Your ns variable's scope is for your simpleFlv class so you cannot access it directly from your main application. Put your playPause function in the simpleFlv class package and then you can call the function with your button like this: click="_video.playPause()"

          Vygo
          • 2. Re: Beginner Question: controlling AS3 with Flex button
            JaxonRice Level 1
            Hy Vygo

            Thanks for pointing me in the right direction. I am still having some problems though - could you give me an example of where I would put the playpause function in my simpleFlv class?

            Thanks once again
            • 3. Re: Beginner Question: controlling AS3 with Flex button
              chris.huston.t10 Level 3
              Try this to add simple pause and resume features for a video

              <?xml version="1.0" encoding="utf-8"?>
              <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">
              <mx:Script>
              <![CDATA[
              import flash.display.Sprite;

              public var _video:simpleFlv = new simpleFlv();

              public function init():void
              {
              rawChildren.addChildAt(_video,2);
              }
              ]]>
              </mx:Script>
              <mx:Button label="Pause" click="_video.playPause()" y="10" x="403"/>
              <mx:Button x="472" y="10" label="Resume" click="_video.resumeVid()"/>

              </mx:Application>


              package
              {
              import flash.display.Sprite;
              import flash.net.NetConnection;
              import flash.media.Video;
              import flash.net.NetStream;
              import flash.events.NetStatusEvent;
              import flash.display.Stage;
              import flash.events.AsyncErrorEvent;

              public class simpleFlv extends Sprite
              {
              private var ns:NetStream;
              public function simpleFlv(){
              var nc:NetConnection= new NetConnection();
              nc.connect(null);
              ns = new NetStream(nc);
              var vid:Video = new Video(320,240);
              this.addChild(vid);
              vid.attachNetStream(ns);
              vid.smoothing=true;
              ns.play("foo.flv");
              ns.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
              }

              private function asyncErrorHandler(event:AsyncErrorEvent):void {
              // ignore AsyncErrorEvent events.
              }

              public function playPause():void {
              ns.pause();
              }
              public function resumeVid():void {
              ns.resume();
              }

              }
              }

              Vygo
              • 4. Re: Beginner Question: controlling AS3 with Flex button
                JaxonRice Level 1
                Thank you so much for taking the time to answer this for me. It works perfectly.