It's an example of using NetConnection and NetStream to play music on a Flash Media Server, along with comparison to how it was done in AS2. See inline comments.
/**
* Copyright wh0 ( http://wonderfl.net/user/wh0 )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/mZFj
*/
package {
import flash.media.Sound;
import flash.net.NetStream;
import flash.net.ObjectEncoding;
import com.adobe.serialization.json.JSON;
import flash.events.NetStatusEvent;
import flash.net.NetConnection;
import flash.events.UncaughtErrorEvent;
import com.actionscriptbible.Example;
public class FlashTest extends Example {
private var nc:NetConnection;
private var ns:NetStream;
public function FlashTest() {
loaderInfo.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR, function(e:UncaughtErrorEvent):void { trace(e.error); });
nc = new NetConnection();
// An AS3 movie defaults to AMF3. This server only supports AMF0.
nc.objectEncoding = ObjectEncoding.AMF0;
// The client makes callbacks available to the server.
nc.client = new TraceClient(trace);
// Most events are dispatched as "netStatus". This used to be onStatus in AS2.
nc.addEventListener(NetStatusEvent.NET_STATUS, netStatus);
// NetConnection seems to work fine cross-domain (:
nc.connect('rtmp://stream.apmmusic.com/myapm_stream_app/');
}
private function netStatus(e:NetStatusEvent):void {
switch (e.info.code) {
case 'NetConnection.Connect.Success':
// Connected okay.
trace('connected (:');
success();
break;
default:
trace('unhandled netStatus ' + JSON.encode(e.info));
break;
}
}
private function success():void {
ns = new NetStream(nc);
// Most events are dispatched as "netStatus". This used to be onStatus in AS2.
ns.addEventListener(NetStatusEvent.NET_STATUS, netStatus);
// Buffer 2 seconds of data before playing. This used to be setBufferTime() in AS2.
ns.bufferTime = 2;
// Play a remote file:
// name = format:path (no extension)
// start = 0 (recorded stream from start)
// len = -1 (until end)
// reset = true (clear playlist and play immediately)
ns.play('mp3:audio/KPM/KPM_KPM_0768/KPM_KPM_0768_00101', 0, -1, true);
// We don't attach it to a Sound object in AS3. It just plays...
}
}
}
import flash.utils.Proxy;
import flash.utils.flash_proxy;
class TraceClient extends Proxy {
private var trace:Function;
public function TraceClient(trace:Function) {
this.trace = trace;
}
flash_proxy override function getProperty(name:*):* {
trace('getting ' + name);
return function (...rest):* {
trace('calling ' + name + '(' + rest.join(', ') + ')');
};
}
}