0

This my first time using StackExchange so I apologize if I miss anything.

I am trying to create a AS3 Flash that will record a video using a webcam and RED5 media server; I am stuck (I am not a programmer, more of a computer handyman that does everything). The example that comes with RED5 works fine (though is in AS2 and I could not, for some reason, make certain things I need to do work), but my code doesnt seem to record the stream as there is not file, the RED5 console only says:

[INFO] [NioProcessor-3] org.red5.server.adapter.ApplicationAdapter - File lecture.flv was deleted

here is the code so far. (updated 09/07/12)

import flash.display.Sprite;
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.media.Camera;
import flash.events.MouseEvent;
import flash.media.Microphone;
import flash.events.*;
import flash.media.Video;

var _cam:Camera
var _mic:Microphone

// create basic netConnection object
var _nc:NetConnection = new NetConnection();

_nc.client = this
// connect to the local Red5 server
_nc.connect("rtmp://localhost/myapp");
_nc.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);

//Add listeners for buttons
record_btn.addEventListener( MouseEvent.CLICK, recordvid );
stop_btn.addEventListener( MouseEvent.CLICK, stopvideo );
//submit_btn.addEventListener( MouseEvent.CLICK, onSubmit );
//Listeners

function netStatusHandler(event:NetStatusEvent):void{
trace("start netstatus handler");
if (event.info.code == "NetConnection.Connect.Success"){
   attachCamera();
                                                      }
}

function attachCamera(e:Event = null):void {
    trace("attach");
        //Attach Camera to field
        _cam=Camera.getCamera();
        _mic=Microphone.getMicrophone()
        vid.attachCamera(_cam);

}

function stopvideo(e:MouseEvent):void {
    //_ns.close();
}

function recordvid(e:MouseEvent):void {
    var _ns:NetStream = new NetStream(_nc);
     trace("publish");
     _ns.attachCamera(_cam);
     _ns.attachAudio(_mic);
     _ns.publish("lecture", "record");
}

2 Answers2

0

You have to connect & wait for the successful status before publishing the stream.

For Example :

var nc:NetConnection = new NetConnection();
nc.connect("rtmp://fms.example.com/lectureseries");
nc.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);

function netStatusHandler(event:NetStatusEvent):void{
   if (event.info.code == "NetConnection.Connect.Success"){
         var ns:NetStream = new NetStream(nc);
         ns.publish("lecture", "record");
   }
}

Have a look at the Netstream documentation to learn more.

loxxy
  • 12,990
  • 2
  • 25
  • 56
  • Updated the code (as in my post) but I still get the same error, thank you for the help though, I didn't know how to make it so it will wait for the connection to be established at first; so your post did help me a little. – Israel Ramos Oct 07 '12 at 04:13
0

I just found the answer thru extreme googleing, I needed to declare the Netstream variable outside the function; otherwise the "publish" video was "empty" as the garbage collector was destroying my variable at some point.

so outside a function I declare

var _ns:NetStream;

and inside the function I declare:

function recordvid(e:MouseEvent):void {
     _ns = new NetStream(_nc);
     _ns.attachCamera(_cam);
     _ns.attachAudio(_mic);
     _ns.publish("lecture", "record");

Awesomely enough, I found the answer right here in stackoverflow

Community
  • 1
  • 1