音视频流的发布(服务端)
1 package 2 { 3 import flash.display.Sprite; 4 import flash.events.NetStatusEvent; 5 import flash.media.Camera; 6 import flash.media.Microphone; 7 import flash.net.NetConnection; 8 import flash.net.NetStream; 9 import flash.net.ObjectEncoding; 10 11 /** 12 * @author Frost.Yen 13 * @E-mail 871979853@qq.com 14 * @create 2015-7-7 下午2:47:40 15 * 16 */ 17 public class LiveFMS extends Sprite 18 { 19 private var _netConnection:NetConnection = new NetConnection(); 20 private var _netStream:NetStream = null; 21 private var _fms:String = "rtmp://localhost/HelloServer";//"rtmp:/HelloServer" 22 private var _camera:Camera = null; 23 private var _microphone:Microphone = null; 24 25 public function LiveFMS() 26 { 27 init(); 28 } 29 private function init():void 30 { 31 _netConnection.objectEncoding = ObjectEncoding.AMF3; 32 _netConnection.connect(_fms); 33 _netConnection.addEventListener(NetStatusEvent.NET_STATUS,onNetStatus); 34 } 35 36 private function onNetStatus(e:NetStatusEvent):void 37 { 38 trace(e.info.code); 39 switch(e.info.code) 40 { 41 case "NetConnection.Connect.Success"://连接尝试成功 42 publishStream();//发布流到服务器 43 break; 44 case "NetConnection.Connect.Closed"://成功关闭连接 45 46 break; 47 default: 48 break; 49 } 50 } 51 private function publishStream():void 52 { 53 _netStream = new NetStream(_netConnection);//创建于FMS连接相关的音视频数据流对象 54 _camera = Camera.getCamera("0");//获得默认摄像头 55 _camera.setMode(320,240,15);//经试验:320*240效果较佳而占用网络流量较小,15为keyFrame 56 _camera.setQuality(100*1000,90);//100*1000为上传带宽,90为图像质量; 57 _microphone = Microphone.getMicrophone();//获得麦克风对象 58 _microphone.setUseEchoSuppression(true);//抑制回音; 59 _netStream.attachAudio(_microphone);//将音频加入到流中; 60 _netStream.attachCamera(_camera);//将视频加入到流中; 61 _netStream.publish("my_video","live");//将音视频数据发布到FMS服务器上,流名称为:my_video,发布类型为:live(实时); 62 } 63 64 } 65 }
音视频流的播放(客户端)
1 package 2 { 3 import flash.display.SimpleButton; 4 import flash.display.Sprite; 5 import flash.events.MouseEvent; 6 import flash.events.NetStatusEvent; 7 import flash.media.Video; 8 import flash.net.NetConnection; 9 import flash.net.NetStream; 10 import flash.net.ObjectEncoding; 11 import flash.text.TextField; 12 13 /** 14 * @author Frost.Yen 15 * @E-mail 871979853@qq.com 16 * @create 2015-7-10 下午3:03:11 17 * 18 */ 19 [SWF(width="800",height="600")] 20 public class LiveFMSClient extends Sprite 21 { 22 private var _playBtn:Sprite; 23 private var _netConnection:NetConnection = new NetConnection(); 24 private var _netStream:NetStream; 25 private var _fms:String = "rtmp://192.168.3.106/HelloServer"; 26 public function LiveFMSClient() 27 { 28 initView(); 29 } 30 private function initView():void 31 { 32 _playBtn = new Sprite(); 33 _playBtn.graphics.beginFill(0xcccccc); 34 _playBtn.graphics.drawRoundRect(0,0,50,20,5,5); 35 _playBtn.graphics.endFill(); 36 _playBtn.x = _playBtn.y = 50; 37 _playBtn.buttonMode = true; 38 var t:TextField = new TextField(); 39 t.text = "Play"; 40 t.autoSize = "left"; 41 t.mouseEnabled = false; 42 t.x = 0.5*(_playBtn.width-t.width); 43 t.y = 0.5*(_playBtn.height-t.height) 44 _playBtn.addChild(t); 45 this.addChild(_playBtn); 46 _playBtn.addEventListener(MouseEvent.CLICK,onPlay); 47 } 48 49 private function onPlay(e:MouseEvent):void 50 { 51 //_netConnection.objectEncoding = ObjectEncoding.AMF3; 52 _netConnection.connect(_fms); 53 _netConnection.addEventListener(NetStatusEvent.NET_STATUS,onNetStatus); 54 } 55 private function onNetStatus(e:NetStatusEvent):void 56 { 57 trace(e.info.code); 58 switch(e.info.code) 59 { 60 case "NetConnection.Connect.Success": 61 playStream(); 62 break; 63 case "NetConnection.Connect.Closed": 64 65 break; 66 67 default: 68 69 break; 70 } 71 } 72 73 private function playStream():void 74 { 75 _netStream = new NetStream(_netConnection); 76 _netStream.bufferTime = 15; 77 var video:Video = new Video(); 78 video.attachNetStream(_netStream); 79 video.smoothing = true; 80 this.addChild(video); 81 video.x = 80;video.y = 100; 82 _netStream.play("my_video"); 83 _netStream.addEventListener(NetStatusEvent.NET_STATUS,onStreamStatus); 84 } 85 private function onStreamStatus(e:NetStatusEvent):void 86 { 87 trace(e.info.code); 88 switch(e.info.code) 89 { 90 case "NetStream.Play.Start": 91 92 break; 93 case "NetStream.Play.StreamNotFound": 94 trace("无法找到传递给 play() 方法的 FLV。"); 95 break; 96 case "NetStream.Play.UnpublishNotify": 97 trace("服务器取消流的发布"); 98 break; 99 default: 100 101 break; 102 } 103 } 104 } 105 }