Getting Started > Publishing and playing a stream > Recreating the sample

 

Recreating the sample

This sample assumes you are using the Flash MX authoring environment on the computer that is running the Flash Communication Server. If you aren't, substitute your server URL for "localhost" in the following steps. The sample also assumes that the service is running.

 
To create the user interface for this sample:

1

In the Flash authoring environment, select File > New to open a new file.

2

If the Library panel isn't visible, select Window > Library to display it.

3

Add an embedded Video object to the library by clicking the Options menu at the upper right of the Library panel and choosing New Video.

4

Drag two embedded Video objects from the library onto the Stage and, in the Property inspector, name them Live_video and Published_video.

5

Resize the Published_video video object to 200 x 150.

6

If you haven't already done so, create a directory named doc_connect in the applications directory. Save the file as doc_connect.fla in this directory. (By default, the applications directory is in the Flash Communication Server installation directory).

 
To write the ActionScript for this sample:

1

Select the keyframe (frame 1) in the Timeline and open the Actions panel (Window > Actions).

Note: While re-creating the sample, make sure all of the ActionScript is attached to this first keyframe, and not to the objects you create on the Stage.

2

Get the default camera and attach it to the Live_video embedded video object.

client_cam = Camera.get();
Live_video.attachVideo(client_cam);

3

Create a connection function that connects to the Flash Communication Server, displays a trace message indicating whether the connection was successful, and opens an instance of the doc_connect application named room_01. Remember, you must specify the Real-Time Messaging Protocol, rtmp.

function doConnect() {

	client_nc = new netConnection();
	client_nc.onStatus = function(info) {
		trace("Level: " + info.level + "   Code: " +  info.code);
	}
	client_nc.connect("rtmp://localhost/doc_connect/room_01");
	
}

Note: If your SWF is on the same computer that is running the Flash Communication Server, you can use rtmp:/doc_connect/test as a shortcut version of rtmp://localhost/doc_connect/test. This usage indicates a relative path and lets you move the files to a different server without changing the code. Also, remember that if you aren't using the Flash MX authoring environment on the computer that is running the Flash Communication Server, substitute your server URL for "localhost".

4

Create a function to publish the video by creating a network stream out_ns, attaching the camera to that stream, and then publishing the stream as myTestStream.

function publishMe() {
	
	out_ns = new netStream(_root.client_nc);
	out_ns.attachVideo(client_cam);
	out_ns.publish("myTestStream");
	
}

Note: The statement out_ns.publish("myTestStream") omits the optional parameter howToPublish. When this parameter is missing, the server automatically publishes the stream as live (that is, the stream is not recorded while it is being published).

5

Create a function to play the published stream by creating a second network stream in_ns, attaching the contents of that stream to the Published_video video object, and then playing the stream named myTestStream that is being published by the server.

function playMe() {
	
	in_ns = new netStream(_root.client_nc);
	Published_video.attachVideo(in_ns);
	in_ns.play("myTestStream");

}

6

Write the commands to call the functions you just created.

// Connect to the server
doConnect();

// Publish the live stream
publishMe();

// Play back the stream from the server
playMe();

7

Save the file.

 
To test your sample application:

1

Choose File > Publish Settings, select Flash and HTML, click Publish, and then click OK.

2

Choose Control > Test movie.

You see two video windows on the Stage, each displaying the same image.

3

To see how the movie looks in a browser, choose File > Publish Preview > Default, or press Control+F12 (Windows) or Command+F12 (Macintosh).

This sample shows how just a few lines of code can implement client-server communications using Flash Communication Server.

Now that you have set up your development environment and have created your first communication application, you can continue to learn about Flash Communication Server using these resources:

The Welcome page in Flash Communication Server contains tutorials for building simple applications and allows you to view more advanced sample applications.

Flash Communication Server architecture overview provides a more detailed conceptual overview of the product.

Using Communication Objects explains the Flash Communication Server's client-side, server-side, and shared objects.

Debugging and Monitoring Applications shows how to use the debugging tools that come with Flash Communication Server.

Using Communication Components provides step-by-step instructions on how to make complete applications with the communication components.