不好意思,让大家久等了!今天我要实现的一个功能是异步录像!先讲下为什么要提出这样一个问题----假设被监控区域出现了异常(一个陌生人进入),那么本监控系统就要对这一段时间发生的一切进行录制,但此同时我们又在另一端观测这一段时间的实时视频,如何才能做到这一点-----既能录像又不打断我们观测实时视频?
解决原理:我们可以制作发布两个视频流,一个视频流的作用仍然是我们上篇文章所讲到的“把实时视频流传送到FMS服务器”,另外一个视频流的作用则是播放此实时视频流,假如发生异常则利用此视频流进行录像,这样就解决了第一段落提出的问题。 下面的代码可以覆盖上篇文章提到的server.mxml
- <?xml version="1.0" encoding="utf-8"?>
- <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" height="571">
- <mx:Script>
- <![CDATA[
- import mx.core.UIComponent;
- import flash.media.Microphone ;
- import mx.controls.Alert ;
- import flash.display.Graphics ;
- //测试网络可行性
- private var conne:NetConnection = new NetConnection() ;
- private function xianshi():void {
- //必须 在AS3.0中默认的ObjectEncoding为AMF3,但是FMS不支持AMF3,所以
- //要显示的声明为AMF0
- conne.objectEncoding = ObjectEncoding.AMF0 ;
- conne.connect("rtmp://127.0.0.1/example") ;
- conne.addEventListener(NetStatusEvent.NET_STATUS,chuli) ;
- }
- private var nnnns:NetStream = null ;//专门用来向连接到服务器的客户端提供共享视频流
- private var nnnnns:NetStream = null ;//分支视频流 专门用来录像
- private var time:String = null ;//定义时间寄存器
- private function chuli(e:NetStatusEvent):void {
- var result:String = e.info.code ;
- switch(result) {
- case "NetConnection.Connect.Success":
- vd.attachCamera(Camera.getCamera()) ;
- nnnns = new NetStream(conne) ;
- nnnns.attachAudio(Microphone.getMicrophone()) ;
- nnnns.attachCamera(Camera.getCamera()) ;
- //播放server端的视频流(实时视频流,供client端播放)
- nnnns.publish("wwww","live") ;
- nnnnns = new NetStream(conne) ;
- nnnnns.attachAudio(Microphone.getMicrophone()) ;
- nnnnns.attachCamera(Camera.getCamera()) ;
- time = new Date().getTime().toString() ;
- nnnnns.publish(time,"live") ;//记录当前时间,以此作为标志位,发布到FMS
- nnnnns.addEventListener(NetStatusEvent.NET_STATUS,netStreamHandler) ;
- break;
- case "NetStream.Play.StreamNotFound":
- Alert.show("失败") ;
- break;
- default :
- Alert.show("缺省") ;
- break ;
- }
- }
- //停止播放
- private function tingzhi():void {
- nnnns.close() ;
- nnnnns.close() ;
- conne.close() ;
- }
- private function bofang():void {
- nnnns = new NetStream(conne) ;
- var v:Video = new Video() ;
- v.attachNetStream(nnnns) ;
- v.width = 276 ;
- v.height = 202 ;
- vv.addChild(v) ;
- nnnns.play("1199001232984") ;
- }
- private function luxiang():void {
- time = new Date().getTime().toString() ;
- nnnnns.publish(time,"record") ;
- }
- //停止录像
- private function stopLuxiang():void {
- nnnnns.close() ;
- }
- //NetStream事件处理器
- private function netStreamHandler(e:NetStatusEvent):void {
- var s:String = e.info.code ;
- switch(s) {//停止录像时,再记录当前时间,并以此为标志位,发不到FMS
- case "NetStream.Record.Stop" :
- nnnnns = new NetStream(conne) ;
- nnnnns.attachAudio(Microphone.getMicrophone()) ;
- nnnnns.attachCamera(Camera.getCamera()) ;
- time = new Date().getTime().toString() ;
- nnnnns.publish(time,"live") ;
- break ;
- }
- }
- //拍照功能实现
- private function paizhao():void {
- var bmp:BitmapData = new BitmapData(vd.width,vd.height,true,0) ;
- bmp.draw(vd) ;
- var bitMap:Bitmap = new Bitmap(bmp) ;
- image.source = bitMap ;
- }
- ]]>
- </mx:Script>
- <mx:VideoDisplay x="0" y="0" width="264" height="213" id="vd"/>
- <mx:Button label="连接服务器" click="xianshi()" x="10" y="247">
- </mx:Button>
- <mx:Button click="tingzhi()" x="99" y="247" width="125" height="21" label="断开与服务器连接">
- </mx:Button>
- <mx:VideoDisplay x="285" y="11" width="276" height="202" id="vv"/>
- <mx:Button x="458" y="247" label="播放" click="bofang()"/>
- <mx:Button x="302" y="247" label="开始录像" click="luxiang()"/>
- <mx:Button x="380" y="247" label="停止录像" click="stopLuxiang()"/>
- <mx:Button x="229" y="247" label="拍照" click="paizhao()"/>
- <mx:Image x="45" y="314" width="264" height="213" id="image"/>
- </mx:Application>
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" height="571">
<mx:Script>
<![CDATA[
import mx.core.UIComponent;
import flash.media.Microphone ;
import mx.controls.Alert ;
import flash.display.Graphics ;
//测试网络可行性
private var conne:NetConnection = new NetConnection() ;
private function xianshi():void {
//必须 在AS3.0中默认的ObjectEncoding为AMF3,但是FMS不支持AMF3,所以
//要显示的声明为AMF0
conne.objectEncoding = ObjectEncoding.AMF0 ;
conne.connect("rtmp://127.0.0.1/example") ;
conne.addEventListener(NetStatusEvent.NET_STATUS,chuli) ;
}
private var nnnns:NetStream = null ;//专门用来向连接到服务器的客户端提供共享视频流
private var nnnnns:NetStream = null ;//分支视频流 专门用来录像
private var time:String = null ;//定义时间寄存器
private function chuli(e:NetStatusEvent):void {
var result:String = e.info.code ;
switch(result) {
case "NetConnection.Connect.Success":
vd.attachCamera(Camera.getCamera()) ;
nnnns = new NetStream(conne) ;
nnnns.attachAudio(Microphone.getMicrophone()) ;
nnnns.attachCamera(Camera.getCamera()) ;
//播放server端的视频流(实时视频流,供client端播放)
nnnns.publish("wwww","live") ;
nnnnns = new NetStream(conne) ;
nnnnns.attachAudio(Microphone.getMicrophone()) ;
nnnnns.attachCamera(Camera.getCamera()) ;
time = new Date().getTime().toString() ;
nnnnns.publish(time,"live") ;//记录当前时间,以此作为标志位,发布到FMS
nnnnns.addEventListener(NetStatusEvent.NET_STATUS,netStreamHandler) ;
break;
case "NetStream.Play.StreamNotFound":
Alert.show("失败") ;
break;
default :
Alert.show("缺省") ;
break ;
}
}
//停止播放
private function tingzhi():void {
nnnns.close() ;
nnnnns.close() ;
conne.close() ;
}
private function bofang():void {
nnnns = new NetStream(conne) ;
var v:Video = new Video() ;
v.attachNetStream(nnnns) ;
v.width = 276 ;
v.height = 202 ;
vv.addChild(v) ;
nnnns.play("1199001232984") ;
}
private function luxiang():void {
time = new Date().getTime().toString() ;
nnnnns.publish(time,"record") ;
}
//停止录像
private function stopLuxiang():void {
nnnnns.close() ;
}
//NetStream事件处理器
private function netStreamHandler(e:NetStatusEvent):void {
var s:String = e.info.code ;
switch(s) {//停止录像时,再记录当前时间,并以此为标志位,发不到FMS
case "NetStream.Record.Stop" :
nnnnns = new NetStream(conne) ;
nnnnns.attachAudio(Microphone.getMicrophone()) ;
nnnnns.attachCamera(Camera.getCamera()) ;
time = new Date().getTime().toString() ;
nnnnns.publish(time,"live") ;
break ;
}
}
//拍照功能实现
private function paizhao():void {
var bmp:BitmapData = new BitmapData(vd.width,vd.height,true,0) ;
bmp.draw(vd) ;
var bitMap:Bitmap = new Bitmap(bmp) ;
image.source = bitMap ;
}
]]>
</mx:Script>
<mx:VideoDisplay x="0" y="0" width="264" height="213" id="vd"/>
<mx:Button label="连接服务器" click="xianshi()" x="10" y="247">
</mx:Button>
<mx:Button click="tingzhi()" x="99" y="247" width="125" height="21" label="断开与服务器连接">
</mx:Button>
<mx:VideoDisplay x="285" y="11" width="276" height="202" id="vv"/>
<mx:Button x="458" y="247" label="播放" click="bofang()"/>
<mx:Button x="302" y="247" label="开始录像" click="luxiang()"/>
<mx:Button x="380" y="247" label="停止录像" click="stopLuxiang()"/>
<mx:Button x="229" y="247" label="拍照" click="paizhao()"/>
<mx:Image x="45" y="314" width="264" height="213" id="image"/>
</mx:Application>
请大家注意看下,写注释的地方,那些是本篇文章的核心!(因为代码比较多,因此本人大致做了些删改),有什么不明白的可以留言,本人将会为大家解答!
下篇文章就是本视频监控软件最核心的地方----图像识别,至今为止,本人仍在探寻较为高效的算法。哪位兄弟对此方面的图像识别算法有研究,可以共同交流下!
本文介绍了一种在视频监控系统中实现异步录像的方法。通过创建两个视频流,一个用于实时传输,另一个用于录像,即使在录像过程中也能保证实时视频的连续播放。文章提供了具体的ActionScript 3.0代码示例。

978

被折叠的 条评论
为什么被折叠?



