DekGenius.com
[ Team LiB ] Previous Section Next Section

Recipe 14.7 Subscribing to Audio/Video Content

14.7.1 Problem

You want to subscribe a Flash movie to audio and/or video content from a FlashCom server.

14.7.2 Solution

Create a net stream that uses an existing net connection to the FlashCom server and invoke the NetStream.play( ) method.

14.7.3 Discussion

FlashCom audio and video are transferred between the client and server using net streams. When a Flash movie accesses a net stream of a FlashCom application, it is said to be subscribed to that net stream.

The NetStream class, from which you can create net stream objects, facilitates the sending and receiving of audio and video. You should use a net stream object to subscribe to any audio or video content from a FlashCom server. The same code is used to receive the stream whether that content has been previously recorded or whether it is live. When you create a net stream object, you must specify the net connection object (see Recipe 14.2) over which the stream should be sent. For example:

// Create the NetConnection object.
myConnection = new NetConnection(  );

// Call the NetConnection.connect(  ) method to connect to an application.
myConnection.connect("rtmp:/myApplication/");

// Create a net stream that uses the net connection myConnection.
subscribe_ns = new NetStream(myConnection);

Once you have created a net stream object, you can use the NetStream.play( ) method to subscribe to any available audio and/or video content on the server. The play( ) method requires you to specify the name of the stream as it was published to the FlashCom application. It is your responsibility to know the name of the stream. Presumably, you either created the movie that published the stream in the first place, or you can discover the information from the developer who did.

subscribe_ns.play("myAVStream");

The preceding example is the simplest form of the play( ) method. When you provide only the name of the stream to play, Flash automatically does the following:

  • Attempts to locate a live stream of the specified name. If the live stream is found, Flash opens the live stream and plays it until it is no longer available.

  • If no live stream is found, Flash looks for a recorded stream of the specified name. If a recorded stream is found, Flash opens the recorded stream and plays it until it ends.

  • If no existing live or recorded stream is found, Flash opens a live stream with the specified name and waits for someone to publish to it.

You can also specify start and length parameters to wield more control over how Flash looks for and plays streams. The start parameter is an optional second parameter with the following possible values:

0 or greater

Looks for a recorded stream only. If no such stream is found, Flash plays the next stream in the playlist, if applicable. If found, Flash begins playback at the specified number of seconds from the beginning of the stream.

-1

Looks for a live stream only. If no live stream is found, the behavior depends on the value for the length parameter. If length is negative, Flash waits indefinitely for someone to publish to the stream. Otherwise, Flash times out after the number of seconds specified by length and moves to the next stream in the playlist, if applicable.

Numbers less than or equal to -2

Same as the default behavior described earlier: looks for a live stream first. If no live stream is found, it looks for a recorded stream. If no recorded stream is found, it opens a live stream and waits for someone to publish to it.

The length parameter is an optional third parameter with the following possible values:

Any negative number

Plays the stream until it ends (for a recorded stream) or until it is no longer available (for a live stream).

0

Plays a single frame at the time specified by the start parameter. This is useful for showing stills from a stream.

Any positive number

Plays the stream for the specified number of seconds or until the stream ends.

If the net stream content includes video content, you must use the attachVideo( ) method to attach the net stream to a video object in order to display the video. Again, it is your responsibility to know whether or not the stream contains video.

myVideo_mc.my_video.attachVideo(subscribe_ns);

You do not need to attach the audio portion of a net stream to a movie clip using attachAudio( ) in order to hear it. However, if you want to be able to control the sound, you must attach it to a movie clip (see Recipe 14.6).

The video and/or audio from a subscribed net stream begins playback as soon as enough has buffered into the Player. The default buffer time for a stream is 0. This means that the stream begins playing immediately (technically, the Flash Player still buffers approximately 10 milliseconds). However, if there is any lag in the connection, unbuffered content is interrupted. To help ensure smooth playback, use the setBufferTime( ) method to specify the number of seconds to buffer before playing the stream. The buffer is ignored for live streams.

// Set the buffer time to 30 seconds.
subscribe_ns.setBufferTime(30);

14.7.4 See Also

Recipe 13.2, and Recipe 14.6 for information on controlling attached audio

    [ Team LiB ] Previous Section Next Section