Birds
package {
import com.adobe.images.JPGEncoder;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.events.NetStatusEvent;
import flash.events.SecurityErrorEvent;
import flash.media.Video;
import flash.net.FileReference;
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.events.Event;
import flash.system.Security;
import flash.utils.ByteArray;
public class Birds extends Sprite {
private var connection:NetConnection;
public function Birds () {
connection = new CustomConnection ();
connection.addEventListener (NetStatusEvent.NET_STATUS, netStatusHandler);
connection.addEventListener (SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
connection.connect ("rtmp://cp81990.live.edgefcs.net/live/");
}
private function netStatusHandler(event:NetStatusEvent):void {
if (event.info.code == "NetConnection.Connect.Success") connectStream ();
}
private function securityErrorHandler(ignore:SecurityErrorEvent):void { }
private var video:Video;
private var stream:NetStream;
private function connectStream():void {
stream = new NetStream(connection);
stream.checkPolicyFile = true;
stream.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
video = new Video();
stream.client = new CustomClient(stage, video);
video.attachNetStream(stream);
stream.play("lucianowildbirds@15357");
addChild(video);
stage.addEventListener(MouseEvent.CLICK, saveScreenshot);
}
private function saveScreenshot (e:MouseEvent):void {
var bmpData:BitmapData = new BitmapData (video.width, video.height);
// security exception here
bmpData.draw (video);
var jpgEncoder:JPGEncoder = new JPGEncoder(80);
var jpgData:ByteArray = jpgEncoder.encode(bmpData);
var fileRef:FileReference = new FileReference;
fileRef.save (jpgData, "bird.jpg");
}
}
}
import flash.net.NetConnection;
class CustomConnection extends NetConnection {
public function CustomConnection () { }
public function onBWDone ():void { }
public function onFCSubscribe (ignore:*):void { }
}
import flash.display.Stage;
import flash.media.Video;
class CustomClient {
public var stage:Stage;
public var video:Video;
public function CustomClient (stage:Stage, video:Video) {
this.stage = stage; this.video = video;
}
public function onMetaData(info:Object):void {
stage.frameRate = info.framerate;
video.x = 0.5 * (stage.stageWidth - info.width); video.width = info.width;
video.y = 0.5 * (stage.stageHeight - info.height); video.height = info.height;
}
public function onCuePoint(ignore:*):void { }
}