音视频流的发布(服务端)
package
{
import flash.display.Sprite;
import flash.events.NetStatusEvent;
import flash.media.Camera;
import flash.media.Microphone;
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.net.ObjectEncoding; /**
* @author Frost.Yen
* @E-mail [email protected]
* @create 2015-7-7 下午2:47:40
*
*/
public class LiveFMS extends Sprite
{
private var _netConnection:NetConnection = new NetConnection();
private var _netStream:NetStream = null;
private var _fms:String = "rtmp://localhost/HelloServer";//"rtmp:/HelloServer"
private var _camera:Camera = null;
private var _microphone:Microphone = null; public function LiveFMS()
{
init();
}
private function init():void
{
_netConnection.objectEncoding = ObjectEncoding.AMF3;
_netConnection.connect(_fms);
_netConnection.addEventListener(NetStatusEvent.NET_STATUS,onNetStatus);
} private function onNetStatus(e:NetStatusEvent):void
{
trace(e.info.code);
switch(e.info.code)
{
case "NetConnection.Connect.Success"://连接尝试成功
publishStream();//发布流到服务器
break;
case "NetConnection.Connect.Closed"://成功关闭连接 break;
default:
break;
}
}
private function publishStream():void
{
_netStream = new NetStream(_netConnection);//创建于FMS连接相关的音视频数据流对象
_camera = Camera.getCamera("0");//获得默认摄像头
_camera.setMode(320,240,15);//经试验:320*240效果较佳而占用网络流量较小,15为keyFrame
_camera.setQuality(100*1000,90);//100*1000为上传带宽,90为图像质量;
_microphone = Microphone.getMicrophone();//获得麦克风对象
_microphone.setUseEchoSuppression(true);//抑制回音;
_netStream.attachAudio(_microphone);//将音频加入到流中;
_netStream.attachCamera(_camera);//将视频加入到流中;
_netStream.publish("my_video","live");//将音视频数据发布到FMS服务器上,流名称为:my_video,发布类型为:live(实时);
} }
}
音视频流的播放(客户端)
package
{
import flash.display.SimpleButton;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.events.NetStatusEvent;
import flash.media.Video;
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.net.ObjectEncoding;
import flash.text.TextField; /**
* @author Frost.Yen
* @E-mail [email protected]
* @create 2015-7-10 下午3:03:11
*
*/
[SWF(width="800",height="600")]
public class LiveFMSClient extends Sprite
{
private var _playBtn:Sprite;
private var _netConnection:NetConnection = new NetConnection();
private var _netStream:NetStream;
private var _fms:String = "rtmp://192.168.3.106/HelloServer";
public function LiveFMSClient()
{
initView();
}
private function initView():void
{
_playBtn = new Sprite();
_playBtn.graphics.beginFill(0xcccccc);
_playBtn.graphics.drawRoundRect(0,0,50,20,5,5);
_playBtn.graphics.endFill();
_playBtn.x = _playBtn.y = 50;
_playBtn.buttonMode = true;
var t:TextField = new TextField();
t.text = "Play";
t.autoSize = "left";
t.mouseEnabled = false;
t.x = 0.5*(_playBtn.width-t.width);
t.y = 0.5*(_playBtn.height-t.height)
_playBtn.addChild(t);
this.addChild(_playBtn);
_playBtn.addEventListener(MouseEvent.CLICK,onPlay);
} private function onPlay(e:MouseEvent):void
{
//_netConnection.objectEncoding = ObjectEncoding.AMF3;
_netConnection.connect(_fms);
_netConnection.addEventListener(NetStatusEvent.NET_STATUS,onNetStatus);
}
private function onNetStatus(e:NetStatusEvent):void
{
trace(e.info.code);
switch(e.info.code)
{
case "NetConnection.Connect.Success":
playStream();
break;
case "NetConnection.Connect.Closed": break; default: break;
}
} private function playStream():void
{
_netStream = new NetStream(_netConnection);
_netStream.bufferTime = 15;
var video:Video = new Video();
video.attachNetStream(_netStream);
video.smoothing = true;
this.addChild(video);
video.x = 80;video.y = 100;
_netStream.play("my_video");
_netStream.addEventListener(NetStatusEvent.NET_STATUS,onStreamStatus);
}
private function onStreamStatus(e:NetStatusEvent):void
{
trace(e.info.code);
switch(e.info.code)
{
case "NetStream.Play.Start": break;
case "NetStream.Play.StreamNotFound":
trace("无法找到传递给 play() 方法的 FLV。");
break;
case "NetStream.Play.UnpublishNotify":
trace("服务器取消流的发布");
break;
default: break;
}
}
}
}