If you have built a Flash video player interface for progressive download video, you may have used the FLVPlayback component to design the video player interface. This tutorial uses a different technique: adding the Video object to the Stage using ActionScript 3.0.
To use the FLVPlayback component with Flash, see:
The easiest way to run the sample is to install it on the same computer as your development server.
RootInstall/applications/Streams
RootInstall/applications/Streams/streams/_definst_/bikes.flv
nc.connect("rtmp://localhost/Streams");
See "Connecting to the server" for details on how to construct the URL.
You can watch the output as the stream plays and the connection status changes. The call to NetStream.play() triggers the call to onMetaData, which displays metadata in the console window, like this:
metadata: duration=30 width=292 height=292 framerate=30
An application window opens in which the video runs. Click the Flex Builder window to see the output messages. The full output looks like this:
connected is: true event.info.level: status event.info.code: NetConnection.Connect.Success Congratulations! you're connected connected is: true event.info.level: status event.info.code: NetStream.Play.Reset connected is: true event.info.level: status event.info.code: NetStream.Play.Start metadata: duration=30 width=292 height=292 framerate=30 [SWF] C:\samples\Streams\bin\Streams-debug.swf - 3,387 bytes after decompression connected is: true event.info.level: status event.info.code: NetStream.Buffer.Full connected is: true event.info.level: status event.info.code: NetStream.Play.Stop The stream has finished playing connected is: true event.info.level: status event.info.code: NetStream.Buffer.Flush handling playstatus here connected is: true event.info.level: status event.info.code: NetStream.Buffer.Empty
package {
import flash.display.Sprite;
import flash.net.NetConnection;
import flash.events.NetStatusEvent;
import flash.net.NetStream;
import flash.media.Video;
...
}
public class Streams extends Sprite
{
var nc:NetConnection;
var stream:NetStream;
var playStream:NetStream;
var video:Video;
...
}
public function Streams()
{
nc = new NetConnection();
nc.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
nc.connect("rtmp://localhost/Streams");
}
private function netStatusHandler(event:NetStatusEvent):void
{
trace("connected is: " + nc.connected );
trace("event.info.level: " + event.info.level);
trace("event.info.code: " + event.info.code);
switch (event.info.code)
{
case "NetConnection.Connect.Success":
trace("Congratulations! you're connected");
connectStream(nc);
// createPlayList(nc);
// instead you can also call createPlayList() here
break;
case "NetConnection.Connect.Failed":
case "NetConnection.Connect.Rejected":
trace ("Oops! the connection was rejected");
break;
case "NetStream.Play.Stop":
trace("The stream has finished playing");
break;
case "NetStream.Play.StreamNotFound":
trace("The server could not find the stream you specified");
break;
case "NetStream.Publish.BadName":
trace("The stream name is already used");
break;
}
}
(To see the full list of event codes that are available, check NetStatusEvent.info in the ActionScript 3.0 Language and Components Reference. )
private function connectStream(nc:NetConnection):void {
stream = new NetStream(nc);
stream.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
stream.client = new CustomClient();
...
Notice that you set the client property to an instance of the CustomClient class. CustomClient is a separate class you need to write that defines some special event handlers (see "Write the client event handler class").
video = new Video();
video.attachNetStream(stream);
Here we create the Video object using ActionScript 3.0. You can also create it by dragging the Video symbol to the Stage in Flash.
In ActionScript 3.0, use Video.attachNetStream()--not Video.attachVideo() as in ActionScript 2.0--to attach the stream to the Video object.
...
stream.play("bikes", 0);
addChild(video);
}
You don't need to call addChild() if you dragged a Video symbol to the Stage using Flash.
The URI of the stream you pass to NetStream.play() is relative to the URI of the application you pass to NetConnection.connect().
Write the client event handler class
You also need to write the CustomClient class, which contains the onMetaData and onPlayStatus event handlers. You must handle these events when you call NetStream.play(), but you cannot use the addEventListener() method to register the event handlers.
stream.client = new CustomClient();
class CustomClient {
}
public function onMetaData(info:Object):void {
trace("metadata: duration=" + info.duration + " width=" + info.width +
" height=" + info.height + " framerate=" + info.framerate);
}
public function onPlayStatus(info:Object):void {
trace("handling playstatus here");
}
Checking video files before playing
Use the FLVCheck tool to check a recorded video file for errors before playing it. Errors in the video file might prevent it from playing correctly. For more information, see Adobe Flash Media Server Configuration and Administration Guide.