To play a stream, use the NetStream class. Create a NetStream object and pass the contructor a NetConnection object:
ns:NetStream = new NetStream(nc);Call the NetStream.play() method or the NetStream.play2() method and pass the URI of the media file:
ns:NetStream = new NetStream(nc);
ns.play("bikes"); ns.play2("bikes");
This code plays the recorded stream named “bikes.flv” within the application to which you are connected with NetConnection.connect().
Different file types require prefixes and file extensions. For example, the following code plays the file “bikes.f4v”,
ns.play("mp4:bikes.f4v");
For detailed information about play() and play2(), their parameters, and when to use each method, see the NetStream class in the ActionScript 3.0 Reference.
To play a stream, specify the codec before you specify the path to the stream. FLV streams don’t require a codec prefix, but F4V/MP4 files, MP3 files, and RAW files do.
Suppose you’re using the default vod application. By default, the vod application is configured to look for streams in the applications/vod/media folder. Suppose a stream called “sample” is nested in the applications/vod/media/final folder. The following examples are for the client-side NetStream.play() method:
ns.play("mp4:final/sample.f4v",0,-1) // F4V/MP4 files ns.play("raw:final/sample",0,-1) // RAW files ns.play("mp3:final/sample",0,-1) //MP3 files ns.play("final/sample",0,-1). // FLV files
myStream.play("mp4:final/sample.f4v",0,-1) // F4V/MP4 files myStream.play("raw:final/sample",0,-1) // RAW files myStream.play("mp3:final/sample",0,-1) // MP3 files myStream.play("final/sample",0,-1) // FLV files
Very low frame rate H.264 videos can take a long time to start if the buffer is too short.
H.264 video requires 64 messages before it begins playback. For example, at 15 fps, 2 seconds of buffer holds 30 samples. In this case, the server and the player wait for over 4 seconds for the 64 images to arrive, even if they're small.
Server-side streams do not play if the file size is less than either the configured buffer time (MinBufferTime tag in the Application.xml configuration file), or 2 seconds.
When playing a stream, set NetStream.bufferTime to at least .1. Set the value smaller for live applications. Set the value larger (3-5 seconds) for on-demand applications.
Use virtual directories to simplify mapping URIs to local and network drives. Virtual directories let you publish and store media files in different, predetermined locations, which can help you organize your media files. Configure virtual directories in the VirtualDirectory/Streams tag of the Vhost.xml file.
One way you can use directory mapping is to separate storage of different kinds of resources. For example, your application could allow users to view either high-bandwidth video or low-bandwidth video, and you might want to store high-bandwidth and low-bandwidth video in separate folders. You can create a mapping wherein all streams that start with low are stored in a specific directory, C:\low_bandwidth, and all streams that start with high are stored in a different directory:
<VirtualDirectory> <Streams>low;c:\low_bandwidth</Streams> <Streams>high;c:\high_bandwidth</Streams> </VirtualDirectory>
When the client wants to access low-bandwidth video, the client calls ns.play("low/sample"). This call tells the server to look for the sample.flv file in the c:\low_bandwidth folder.
Similarly, a call to ns.play("high/sample") tells the server to look for the sample.flv file in the c:\high_bandwidth folder.
The following table shows three examples of different virtual directory configurations, including mapping to a local drive and a network drive, and how the configurations determine the directory to which a recorded stream is published. In the first case, because the URI specified ("myStream") does not match the virtual directory name that is specified ("low"), the server publishes the stream to the default streams directory.
Mapping in Vhost.xml <VirtualDirectory><Streams> tag |
URI in NetStream call |
Location of published stream |
---|---|---|
low;e:\fmsstreams |
"myStream" |
c:\...\rootinstall\applications\yourApp\streams\_definst_\myStream.flv |
low;e:\fmsstreams |
"low/myStream" |
e:\fmsstreams\myStream.flv |
low;\\mynetworkDrive\share\fmsstreams |
"low/myStream" |
\\mynetworkDrive\share\fmsstreams\myStream.flv |
This feature enables you to get a thumbnail snapshot of a given video, including sound, for display purposes.
Flash Player clients are permitted to access data from streams in the directories specified by the Client.audioSampleAccess and Client.videoSampleAccess properties. See .
To access data, call BitmapData.draw() and SoundMixer.computeSpectrum() on the client. For more information about accessing raw audio, see Accessing raw sound data.
A recorded media file often has metadata encoded in it by the server or a tool. The Flash Video Exporter utility (version 1.1 or later) is a tool that embeds a video’s duration, frame rate, and other information into the video file itself. Other video encoders embed different sets of metadata, or you can explicitly add your own metadata.
The NetStream object that plays the stream on the client dispatches an onMetaData event when the stream encounters the metadata. To read the metadata, you must handle the event and extract the info object that contains the metadata. For example, if a file is encoded with Flash Video Exporter, the info object contains these properties:
duration |
The duration of the video. |
width |
The width of the video display. |
height |
The height of the video display. |
framerate |
The frame rate at which the video was encoded. |
You can deliver Adobe Extensible Metadata Platform (XMP) metadata embedded video streaming through Adobe Media Server to Flash Player. Adobe Media Server supports XMP metadata embedded in FLV and MP4/F4V formats. Adobe Media Server 3.5 supports one XMP metadata packet per MP4/F4V file.
With XMP metadata, you have a communication system that provides critical media information from media creation to the point where media is viewed. XMP information you add during the production process can add to the interactive experience of the media. In addition, speech-to-text metadata embedded within files and encoded from Adobe encoding tools such as Adobe Media Encoder can be delivered. AMF0 and AMF3 connections are supported. XMP metadata can be internal information about the file or information for end users.
For example, you could create a trailer in Adobe® Premiere® and transfer the metadata to the FLV file. When users view the file, they can use Flash Player 10 search to look for metadata and jump to a specific location in the file. When NetStream plays the content, an onXMPData message with the single field data is sent as a callback. The data field contains the entire XMP message from the media file.
For detailed information about XMP, see www.adobe.com/go/learn_fms_xmp_en.
This tutorial uses ActionScript 3.0 to add a Video object to the Stage to display video. For more information about working with video, see the “Working with Video” chapter in ActionScript 3.0 Developer’s Guide at www.adobe.com/go/learn_fms_video_en.
This tutorial provides the simplest example of displaying video for learning. To build a more robust video player, see the Open Source Media Framework.
Note:
This example uses the MediaPlayer sample, MediaPlayer.as, from the rootinstall/documentation/samples folder.
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
-
private function netStatusHandler(event:NetStatusEvent):void{ trace("event.info.level: " + event.info.level + "\n", "event.info.code: " + event.info.code); switch (event.info.code){ case "NetConnection.Connect.Success": // Call doPlaylist() or doVideo() here. doPlaylist(nc); break; case "NetConnection.Connect.Failed": // Handle this case here. break; case "NetConnection.Connect.Rejected": // Handle this case here. break; case "NetStream.Play.Stop": // Handle this case here. break; case "NetStream.Play.StreamNotFound": // Handle this case here. break; case "NetStream.Publish.BadName": trace("The stream name is already used"); // Handle this case here. break; } }
Note:
To see the full list of event codes that are available, see NetStatusEvent.info in the ActionScript 3.0 Reference.
-
private function connectStream(nc:NetConnection):void { ns = new NetStream(nc); ns.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler); ns.client = new CustomClient(); ...
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.