Listing 1
application.onAppStart = function() {
trace("VideoPlayer application started.");
}
application.onConnect = function(clientObj, authObj) {
if (authObj.password == "hackme") {
trace("Client accepted from "+clientObj.ip);
application.acceptConnection(clientObj);
} else {
application.rejectConnection(clientObj);
}
}
Listing 2
var nc = new NetConnection();
nc.connect("rtmp://localhost/videoplayer/myvideos",{password: "hackme"});
var ns = new NetStream(nc);
videoPlayback.attachVideo(ns);
ns.play("myvideo");
Listing 3
private function connectToServer():Void {
trace("Connecting to the server...");
var videoPlayerRef:VideoPlayer = this; // Refers back to this instance
_netConn = new NetConnection();
_netConn.onStatus = function(statusObj:Object):Void {
trace("Got status from _netConn: "+statusObj.code);
switch (statusObj.code) {
case "NetConnection.Connect.Success":
videoPlayerRef.initNetStream();
break;
case "NetConnection.Connect.Failed":
trace("Connection to the server failed!");
break;
// Other status handling should be done here
}
}
_netConn.connect("rtmp://localhost/videoplayer/myvideos",{password: "hackme"});
}
Listing 4
var videoPlayerRef:VideoPlayer = this;
_netStream = new NetStream(_netConn);
_netStream.setBufferTime(1); // Buffer 1 second of video before playback
_netStream.onStatus = function(statusObj:Object):Void {
trace("Got status from _netStream: "+statusObj.code);
switch (statusObj.code) {
case "NetStream.Play.Start":
if (videoPlayerRef._isSeeking) {
videoPlayerRef._isSeeking = false;
if (!videoPlayerRef._isPaused) {
videoPlayerRef.watchProgress(true);
}
} else {
videoPlayerRef._isPaused = false;
videoPlayerRef._videoController.showPauseButton();
videoPlayerRef.watchProgress(true);
}
break;
case "NetStream.Pause.Notify":
videoPlayerRef._isPaused = true;
videoPlayerRef._videoController.showPlayButton();
videoPlayerRef.watchProgress(false);
break;
case "NetStream.Play.Stop":
videoPlayerRef._isStopping = true;
break;
case "NetStream.Buffer.Empty":
if (videoPlayerRef._isStopping) { // If the stream has stopped and the buffer is empty,
playback is complete.
videoPlayerRef.onMediaStop();
}
break;
}
}
Listing 5
Client.prototype.getStreamLength = function(streamName) {
return Stream.length(streamName);
}
Listing 6
var streamLengthResult:Object = {};
streamLengthResult.onResult = function(streamLength:Number):Void {
trace("Got stream length result: "+streamLength);
videoPlayerRef._streamLength = streamLength;
}
_netConn.call("getStreamLength",streamLengthResult,_mediaName);