package { import flash.media.Video; import flash.media.*; import flash.net.NetConnection; import flash.net.NetStream; import flash.display.Sprite; import flash.text.TextField; import flash.events.Event; import flash.events.NetStatusEvent; import flash.events.SecurityErrorEvent; public class Broadcaster extends Sprite { // Streaming URL private var rtmp_url:String = "rtmp://itp.nyu.edu/sve204/live"; private var stream_name:String = "live"; private var nc:NetConnection; private var ns:NetStream; private var mycam:Camera; private var mymic:Microphone; private var myvid:Video; private var displayText:TextField = new TextField(); public function Broadcaster():void { // Enable debugging showing status in textfield showDebug(true); displayText.text = "Starting Up"; // Create connection object nc = new NetConnection(); displayText.text = "Created NetConnection"; // add netstatus event handler nc.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler); displayText.text = "added netstatus event handler"; // add security event handler nc.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler); displayText.text = "added security event handler"; // Connect to FMS nc.connect(rtmp_url); displayText.text = "Connected?"; } private function showDebug(show:Boolean):void { this.addChild(displayText); } private function connectStream():void { // Create netstream object ns = new NetStream(nc); displayText.text = "Created NetStream"; // Get the camera component mycam = Camera.getCamera(); displayText.text = "Got a camera"; // Set the quality of the camera component // bandwidth (Bytes per second), quality (0 to 100) // 600 kbps == 600/8 * 1000 == 75000 // 25600 Bps == ~ 205 kbps mycam.setQuality(25600, 0); displayText.text = "Set the bandwidth to 25600"; // Set the mode of the camera component // width, height, fps mycam.setMode(320,240,15); // Get the microphone component mymic = Microphone.getMicrophone(); displayText.text = "Got a microphone"; // Set the sample rate in kHz mymic.rate = 11; displayText.text = "Set the sample rate to 11kHz"; // Attach the camera to the netstream object ns.attachCamera(mycam); displayText.text = "Attached Camera to netstream object"; // Attach the mic to the netstream object ns.attachAudio(mymic); displayText.text = "Attached Microphone to netstream object"; // Create a video object myvid = new Video(320, 240); displayText.text = "Created video object"; // Add the video object to the stage this.addChild(myvid); displayText.text = "Added video object to stage"; // Could specify myvid location // myvid.x = 100; // myvid.y = 100; // Attach the camera object to the video object myvid.attachCamera(mycam); displayText.text = "Attached camera to video object"; // Publish the stream to the server // could be "record".. ns.publish(stream_name, "record"); displayText.text = "Published NetStream"; } private function netStatusHandler(event:NetStatusEvent):void { switch (event.info.code) { case "NetConnection.Connect.Success": displayText.text = "Connection Success"; connectStream(); break; case "NetStream.Play.StreamNotFound": displayText.text = "Stream not found: " + rtmp_url; trace("Stream not found: " + rtmp_url); break; } } private function securityErrorHandler(event:SecurityErrorEvent):void { displayText.text = "security error " + event; trace("securityErrorHandler: " + event); } } }