[ Team LiB ] |
Recipe 14.7 Subscribing to Audio/Video Content14.7.1 ProblemYou want to subscribe a Flash movie to audio and/or video content from a FlashCom server. 14.7.2 SolutionCreate a net stream that uses an existing net connection to the FlashCom server and invoke the NetStream.play( ) method. 14.7.3 DiscussionFlashCom 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:
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:
The length parameter is an optional third parameter with the following possible values:
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 AlsoRecipe 13.2, and Recipe 14.6 for information on controlling attached audio |
[ Team LiB ] |