DekGenius.com
[ Team LiB ] Previous Section Next Section

Recipe 14.9 Recording and Publishing Video and Audio

14.9.1 Problem

You want to record video and/or audio from a Flash movie and publish it to a FlashCom server.

14.9.2 Solution

Create a net stream, attach the video using attachVideo( ), and attach the audio using attachAudio( ). Then call the NetStream.publish( ) method and specify "record" or "append" as the recording mode.

14.9.3 Discussion

To use a net stream object to record and publish video and/or audio to a FlashCom server, follow these steps:

  1. Establish a net connection:

    myConnection = new NetConnection(  );
  2. Connect to the FlashCom server:

    myConnection.connect("rtmp:/myApplication/");
  3. Create the net stream:

    publishRecorded_ns = new NetStream(myConnection);
  4. Attach the audio and/or video to the net stream using the attachAudio( ) and/or attachVideo( ) method:

    myMic_mic = Microphone.get(  );
    myCam_cam = Camera.get(  );
    publishRecorded_ns.attachAudio(myMic_mic);
    publishRecorded_ns.attachVideo(myCam_cam);
  5. Use NetStream.publish( ) to record the stream on the server. The first parameter is the name by which the stream should be known by the FlashCom application (when a client wants to subscribe to it, for example). The second parameter is the recording mode. If you specify "record" as the recording mode, any existing stream of the same name (within the FlashCom application) is overwritten. If you specify "append" as the recording mode, the new stream data is appended to any existing stream of the same name (a new stream is created on the server if none exists).

    publishRecorded_ns.publish("myFirstStream", "record");
  6. Call NetStream.publish( ) again with the value false when you want to stop recording the stream:

    publishRecorded_ns.publish(false);

Even if your FlashCom application permits multiple people to record to a single stream, only one user at a time can record to that stream. Additionally, if a recorded stream is being played by a subscribing client, FlashCom locks the stream, preventing you from recording to it. In both of these scenarios, you can catch these errors with an onStatus( ) method. The net stream's onStatus( ) method is invoked whenever there is a status update related to the stream. Flash passes an info object parameter containing information about the status update to the onStatus( ) handler. When another user is recording to a stream, the code property of the info object has the value "NetStream.Publish.BadName"; when the stream is being played, the code property has the value "NetStream.Record.NoAccess".

Here is an example with an onStatus( ) handler that detects these error codes:

myConnection = new NetConnection(  );
myConnection.connect("rtmp:/myApplication/");
publishRecorded_ns = new NetStream(myConnection);

publishRecorded_ns.onStatus = function (infoObj) {
  if (infoObj.code == "NetStream.Publish.BadName") {
    trace("Someone is already recording to this stream");
  } else if (infoObj.code == "NetStream.Record.NoAccess") {
    trace("Someone is playing this stream");
  } else {
    trace("Stream accessed successfully.");
  }
};

myMic_mic = Microphone.get(  );
myCam_cam = Camera.get(  );
publishRecorded_ns.attachAudio(myMic_mic);
publishRecorded_ns.attachVideo(myCam_cam);
publishRecorded_ns.publish("myFirstStream", "record");

14.9.4 See Also

Recipe 14.10. For more information on client-side NetStream info object codes, see http://download.macromedia.com/pub/flashcom/documentation/FlashCom_CS_ASD.pdf.

    [ Team LiB ] Previous Section Next Section