Developing Android Applications with Adobe AIR [73]
By default, the application uses an input buffer. To modify the default buffering time, use the following:
var stream:NetStream = new NetStream(connection);
stream.bufferTime = 5; // value in seconds
When using a streaming server, managing bandwidth fluctuation is a good strategy:
var stream:NetStream = new NetStream(connection);
stream.addEventListener(NetStatusEvent.NET_STATUS, netStreamEvent);
function netStreamEvent(event:NetStatusEvent):void {
var buffTime:int;
swith(event.info.code) {
case "NetStream.Buffer.Full" :
buffTime = 15.0;
break;
case "NetStream.Buffer.empty" :
buffTime = 2.0;
break;
}
stream.bufferTime = buffTime;
}
Read Fabio Sonnati’s article on using dual-threshold buffering, at http://www.adobe.com/devnet/flashmediaserver/articles/fms_dual_buffering.html.
RTMP Streaming
Real Time Messaging Protocol (RTMP) is a protocol using a streaming server such as Flash Media Server or a streaming service such as Influxis or Flash Media Server for Amazon Web Services.
Streaming uses a lot of data. Inform your users to use WiFi so that it is not too costly and guarantees the best possible quality experience.
RTMP server
Let’s use our RTMP server to stream an on-demand video. As in progressive downloading, streaming uses a Video, a NetConnection, and a NetStream object.
NetConnection connects to the streaming server. The protocol used is rtmp. Note the streamClient variable. You need it for callbacks; otherwise, you will get a runtime error:
static const SERVER:String = "rtmp://SERVER_URI/vod/;
static const VIDEO_PATH:String = "/myVideo";
video = new Video();
video.width = 480;
video.height = 320;
connection.addEventListener(NetStatusEvent.NET_STATUS, onNetEvent);
connection.connect(SERVER);
function netConnectionEvent(event:NetStatusEvent):void {
if (event.info.code == "NetConnection.Connect.Success") {
var stream:NetStream = new NetStream(connection);
stream.addEventListener(NetStatusEvent.NET_STATUS, onStreamEvent);
var streamClient:Object = new Object();
streamClient.onMetaData = onMetaData;
stream.client = streamClient;
video.attachNetStream(stream);
stream.play(VIDEO_PATH);
addChild(video);
break;
}
}
function onStreamEvent(event:NetStatusEvent):void {}
function onMetaData(info:Object):void {}
function onBWDone():void {}
It is a good idea to add listeners for connection errors while debugging and to inform your audience in case of an issue:
connection.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
connection.addEventListener
(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
connection.addEventListener(AsyncErrorEvent.ASYNC_ERROR, onAsyncError);
function onIOError(event:IOErrorEvent):void {}
function onSecurityError(event:SecurityErrorEvent):void {}
function onASyncError(event:AsyncErrorEvent):void {}
Local Flash Media Server
You can install a local Flash Media Server to run and test your applications. Change the path to your local server. To ensure the video plays, turn off .swf verifications on the server:
static const SERVER:String = "rtmp://localhost/vod/";
Flash Media Server offers features to examine and monitor your video streams. The Quality of Service API, for instance, returns the user’s current bandwidth. You can extend the functionality of your video management by writing additional server code.
HTTP Dynamic Streaming
Adobe’s adaptive streaming delivery method, called HTTP Dynamic Streaming, is another option for live or on-demand video playback. This method is similar to Apple’s Adaptive Streaming and Microsoft’s Smooth Streaming.
Because HTTP Dynamic Streaming plays back video files in chunks, additional complex logic needs to be built in to put the segments together in the right order and play them back. Therefore, it is recommended that an Open Source Media Framework-based player be used for HTTP Dynamic Streaming.
You can build one from