Application Development Tips and Tricks > Snapshots and thumbnails > Snapshots

 

Snapshots

If you look at the NetStream.attachVideo entry in the Client-Side Communication ActionScript Dictionary, you will see the following usage:

myStream.attachVideo(source | null [, snapShotMilliseconds])

The snapShotMilliseconds parameter is used to send a single snapshot (by providing a value of 0) or a series of snapshots—in effect, time-lapse footage—by providing a positive number that adds a trailer of the specified number of milliseconds to the video feed. When you specify the snapShotMilliseconds parameter, you are controlling how much time elapses during playback between recorded frames.

The following client-side ActionScript code in doc_snapshot.fla connects to the server and plays camera output locally. When the user chooses to take a snapshot, the event handler, doRecord, makes a call to out_ns.attachVideo(client_cam, 0). The second parameter, 0, causes just one frame to be recorded.

#include "NetDebug.as"

// Recording state variable
RecState_box.text = 0;

// Number of snapshots
numSnaps = 0;

// Initialize button label
Record_btn.setLabel("Record");

// Connect to the snapshot app and get connection status 
function doConnect() {
	client_nc = new NetConnection();
	client_nc.onStatus = function(info) {
		trace("Level: " + info.level + newline + "Code: " + info.code);
	};
	client_nc.connect("rtmp:/doc_snapshot/room_01");
}

// Create a stream for recording and getting the snapshot
function initStreams() {
	// Stream for recording
	out_ns = new NetStream(client_nc);
	out_ns.onStatus = function(info) {
		trace("Level: " + info.level + newline + "Code: " + info.code);
	};

	// Stream for playing
	in_ns = new NetStream(client_nc);
	in_ns.onStatus = function(info) {
		trace("Level: " + info.level + newline + "Code: " + info.code);
	};
}

// Get a camera instance and attach it to the local
// video, Live_video, and the output stream
client_cam = Camera.get();
Live_video.attachVideo(client_cam);

// Button event handler for publishing and showing the snapshot
function doRecord() {
	// If you're not recording, begin to record
	if (RecState_box.text == 0) {
		// Clear the snapshot window
		Snapshot_mc.removeMovieClip();
		// Take a snapshot
		out_ns.attachVideo(client_cam, 0);
		out_ns.publish("myRecording"+numSnaps, "record");
		// Set the label to stop
		Record_btn.setLabel("Stop");
		// Update the recording state
		RecState_box.text = 1;
		// If you're recording, stop
	} else {
		// Stop publishing recorded stream
		out_ns.publish(false);
		// Close the stream so that we can use the same to publish again
		out_ns.close();
		// Set the label to "Record"
		Record_btn.setLabel("Record");
		// Update the recording state
		RecState_box.text = 0;
		showSnapshot();
	}
}

// Show the snapshot by attaching the stream to the SnapView_video
// video object and playing it
function showSnapshot() {
	// Create a new movie clip from the exportable movie clip View_mc,
	// which contains a video object, SnapView_video. Provide a new name, and
	// depth (a number relative to the other View_mcs on the stage used
	// to prevent collision)
	_root.attachMovie("View_mc", "Snapshot_mc", numSnaps);
	// Attach the input stream to the SnapView_video video object
	// in the v instance of the View_mc movie
	Snapshot_mc.SnapView_video.attachVideo(in_ns);
	Snapshot_mc._x=375;
	Snapshot_mc._y=225;

	// Play the recording
	in_ns.play("myRecording"+numSnaps);
	// Update the counter for the number of snapshots for next time.
	numSnaps++;
}


// Connect to the server
doConnect();
// Initialize the streams
initStreams();