FluorineFx:视频录制及回放(Flash/AS3环境)
如果不考虑安全因素(指任何人都可连接FluorineFx进行视频录制,而不需要登录认证),其实服务端不用写一行代码,仅需要在apps目录下建一个子目录当作应用,以及在services-config.xml中配置一下rtmp的Channel即可
下面这段flash客户端的as3代码,是从FluorineFx官方的Flash AS2示例修改而来的(当然:只一个示例,细节还有很多可优化的地方)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 | package { import fl.controls.Button; import fl.controls.Label; import fl.controls.TextInput; import fl.controls.CheckBox; import flash.display.Sprite; import flash.utils.Timer; import flash.events.ActivityEvent; import flash.events.TimerEvent; import flash.events.MouseEvent; import flash.events.NetStatusEvent; import flash.events.StatusEvent; import flash.media.Camera; import flash.media.Microphone; import flash.net.NetConnection; import flash.net.NetStream; import flash.media.Video; public class VideoRecord extends Sprite { private var _btnRecord:Button; private var _btnPlay:Button; private var _btnConnect:Button; private var _txtVideoFileName:TextInput; private var _chkAppend:CheckBox; private var _txtServerUrl:TextInput; private var _lblResult:Label; private var _nc:NetConnection = null ; private var _nsPublish:NetStream = null ; private var _nsPlay:NetStream = null ; private var _ncPlay:NetConnection = null ; private var _camera:Camera; private var _microphone:Microphone; private var _videoRecord:Video; private var _videoPlay:Video; private var _videoIsWorked = false ; private var _timer:Timer; public function VideoRecord() { init(); } private function init(): void { this ._btnRecord = this .btnRecord; this ._txtVideoFileName = this .videoFileName; this ._chkAppend = this .chk1; this ._chkAppend. label = "追加" ; this ._btnPlay = btnPlay; this ._btnConnect = btnConnect; this ._txtServerUrl = this .txtServerUrl; this ._lblResult = lblResult; this ._btnRecord.enabled = false ; this ._btnRecord. label = "录制" ; this ._txtVideoFileName.enabled = false ; this ._chkAppend.enabled = false ; this ._btnPlay.enabled = false ; this ._btnPlay. label = "播放" ; this ._btnConnect. label = "连接" ; this ._txtServerUrl.text = "rtmp://localhost/VideoRecording" ; this ._videoRecord = videoRecord; this ._videoPlay = videoPlay; this ._btnConnect.addEventListener(MouseEvent.CLICK, doConnect); } //连接服务器; private function doConnect(e:MouseEvent): void { if ( this ._nc == null ) { this ._nc = new NetConnection ; this ._nc.addEventListener(NetStatusEvent.NET_STATUS, ncNetStatus); } this ._nc.connect( this ._txtServerUrl.text); this ._lblResult.text = "服务器连接中..." ; } //关闭与服务器的连接 private function doCloseConn(e:MouseEvent): void { if ( this ._nc != null ) { if ( this ._nsPublish != null ) { this ._nsPublish.attachCamera( null ); } this ._videoRecord.attachCamera( null ); this ._videoRecord.attachNetStream( null ); this ._videoRecord.clear(); this ._nc.close(); this ._btnConnect. label = "连接" ; this ._btnRecord.enabled = false ; this ._txtVideoFileName.enabled = false ; this ._chkAppend.enabled = false ; this ._videoRecord.clear(); this ._btnConnect.removeEventListener(MouseEvent.CLICK,doCloseConn); this ._btnConnect.addEventListener(MouseEvent.CLICK,doConnect ); stopPublish(); } } //检测conn对象的状态变化 private function ncNetStatus(e:NetStatusEvent): void { //trace(e.info.code); if (e.info.code == "NetConnection.Connect.Success" ) { //连接成功 this ._lblResult.text = "服务器已经连接!" ; this ._btnConnect. label = "断开" ; this ._btnConnect.removeEventListener(MouseEvent.CLICK,doConnect); this ._btnConnect.addEventListener(MouseEvent.CLICK, doCloseConn); this ._camera = Camera.getCamera(); if (_camera == null ) { this ._lblResult.text = "未安装摄像头!" ; return ; } _camera.addEventListener(StatusEvent.STATUS, cameraStatusHandler); _camera.addEventListener(ActivityEvent.ACTIVITY, cameraActivityHandler); this ._videoRecord.attachCamera( this ._camera); //点击"断开"后后,又重新点击"连接"; if (_videoIsWorked) { //恢复控件的可用性; this ._txtVideoFileName.enabled = true ; this ._chkAppend.enabled = true ; this ._btnRecord.enabled = true ; if ( this ._txtVideoFileName.text == "" ) { //this._txtVideoFileName.text = Math.round(Math.random() * 10000).toString(); this ._txtVideoFileName.text = "demo" ; } this ._btnRecord. label = "录制" ; this ._btnRecord.removeEventListener(MouseEvent.CLICK,prepareStopRecord); this ._btnRecord.addEventListener(MouseEvent.CLICK, startRecord); } } else if (e.info.code == "NetConnection.Connect.Closed" ) { this ._lblResult.text = "服务器连接已关闭!" ; } else { this ._lblResult.text = "错误-服务器连接失败!" ; } } //用户选择是否摄像头时触发 function cameraStatusHandler(e:StatusEvent): void { //trace(e); if (e.code == "Camera.Muted" ) { this ._lblResult.text = "您不允许使用摄像头!" ; } else if (e.code == "Camera.Unmuted" ) { this ._lblResult.text = "摄像头视频获取中..." ; _timer = new Timer( 100 , 20 ); //每隔100ms检测摄像头状态,一共检测20次 cameraActivityHandler( null ); } } //摄像头有活动时被触发 private function cameraActivityHandler(e:ActivityEvent): void { //trace("cameraActivityHandler被调用!"); if (! _videoIsWorked) { if (_timer != null ) { _timer.addEventListener(TimerEvent.TIMER, checkCamera); _timer.addEventListener(TimerEvent.TIMER_COMPLETE, checkCameraComplete); _timer.start(); //trace("_timer已经启动!"); } } } //timer回调函数,用于检测摄像头设备是否正确 function checkCamera(e:TimerEvent): void { this ._lblResult.text = "摄像头视频获取中..." ; if ( this ._camera.currentFPS > 0 ) { _timer.stop(); _videoIsWorked = true ; this ._lblResult.text = "摄像头工作正常" ; //恢复控件的可用性; this ._txtVideoFileName.enabled = true ; this ._chkAppend.enabled = true ; this ._btnRecord.enabled = true ; if ( this ._txtVideoFileName.text == "" ) { //this._txtVideoFileName.text = Math.round(Math.random() * 10000).toString(); this ._txtVideoFileName.text = "demo" ; } this ._btnRecord.addEventListener(MouseEvent.CLICK, startRecord); } } //开始录制 private function startRecord(e:MouseEvent): void { //trace("开始录制,_nsPublish=",_nsPublish); if ( this ._nsPublish == null ) { //trace("重新创建ns"); _nsPublish = new NetStream( this ._nc); } this ._nsPublish.attachCamera( this ._camera); this ._nsPublish.publish( this ._txtVideoFileName.text, this ._chkAppend.selected ? "append" : "record" ); this ._nsPublish.addEventListener(NetStatusEvent.NET_STATUS, nsPublishNetStatus); //缓冲20秒; this ._nsPublish.bufferTime = 20 ; } private function nsPublishNetStatus(e:NetStatusEvent): void { //trace(e.info.code); if (e.info.code == "NetStream.Play.StreamNotFound" || e.info.code == "NetStream.Play.Failed" || e.info.code == "NetStream.Publish.BadName" ) { this ._lblResult.text = "推送失败,原因:" + e.info.code; } else if (e.info.code == "NetStream.Record.Start" || e.info.code == "NetStream.Buffer.Empty" ) { //录制开始 this ._btnRecord.removeEventListener(MouseEvent.CLICK, startRecord); this ._btnRecord.addEventListener(MouseEvent.CLICK, prepareStopRecord); this ._lblResult.text = "正在录制..." ; this ._btnRecord. label = "停止" ; //录制时,禁止回放 this ._btnPlay.enabled = false ; //this._btnPlay.label = "播放"; this ._btnPlay.removeEventListener(MouseEvent.CLICK, this .doStopPlay); this ._btnPlay.addEventListener(MouseEvent.CLICK, this .doPlay); } } private function nsPlayNetStatus(e:NetStatusEvent): void { //trace(e.info.code); //失败 if (e.info.code == "NetStream.Play.StreamNotFound" || e.info.code == "NetStream.Play.Failed" ) { } else if (e.info.code== "NetStream.Play.Start" ) { } } private function doStopPlay(e:MouseEvent): void { if ( this ._nsPlay != null ) { this ._videoPlay.attachNetStream( null ); this ._videoPlay.clear(); this ._ncPlay.close(); this ._ncPlay = null ; this ._nsPlay.close(); this ._nsPlay = null ; this ._btnPlay. label = "播放" ; this ._btnPlay.removeEventListener(MouseEvent.CLICK,doStopPlay); this ._btnPlay.addEventListener(MouseEvent.CLICK,doPlay); //trace("已经停止!"); } } function doPlay(e:MouseEvent): void { if ( this ._ncPlay == null ) { this ._ncPlay = new NetConnection(); this ._ncPlay.addEventListener(NetStatusEvent.NET_STATUS,ncPlayNetStatus); this ._ncPlay.connect( this ._txtServerUrl.text); } } private function ncPlayNetStatus(e:NetStatusEvent): void { if (e.info.code == "NetConnection.Connect.Success" ) { if ( this ._nsPlay == null ) { //trace("_nsPlay已经创建!"); this ._nsPlay = new NetStream( this ._ncPlay); this ._nsPlay.addEventListener(NetStatusEvent.NET_STATUS, nsPlayNetStatus); var _client: Object = new Object (); _client.onMetaData = nsPlayOnMetaData; _client.onPlayStatus = nsPlayOnPlayStatus; this ._nsPlay.client = _client; this ._nsPlay.bufferTime = 5 ; this ._nsPlay.play( this ._txtVideoFileName.text); this ._videoPlay.attachNetStream( this ._nsPlay); this ._btnPlay. label = "停止" ; this ._btnPlay.removeEventListener(MouseEvent.CLICK,doPlay); this ._btnPlay.addEventListener(MouseEvent.CLICK,doStopPlay); } } } private function nsPlayOnMetaData(e: Object ): void { trace ( "onmetaData:" + e.duration); } private function nsPlayOnPlayStatus(e: Object ): void { trace ( "onPlayStatus:" + e.code); if (e.code == "NetStream.Play.Complete" ) { trace ( "播放已经停止" ); this ._videoPlay.attachNetStream( null ); this ._videoPlay.clear(); this ._btnPlay. label = "播放" ; this ._btnPlay.removeEventListener(MouseEvent.CLICK,doStopPlay); this ._btnPlay.addEventListener(MouseEvent.CLICK,doPlay); this ._ncPlay.close(); this ._ncPlay = null ; this ._nsPlay.close(); this ._nsPlay = null ; } } //准备停止录制 private function prepareStopRecord(e:MouseEvent) { this ._nsPublish.attachCamera( null ); var _bufferLength = this ._nsPublish.bufferLength; //必须等当前缓冲区中的数据全部发送完以后再正式停止 if (_bufferLength > 0 ) { this ._btnRecord. label = "稍等..." ; this ._lblResult.text = "正在保存,请稍候..." ; //每0.1秒检查一次 _timer = new Timer( 100 ); _timer.addEventListener(TimerEvent.TIMER, doWait); _timer.start(); } else { //trace("停止!"); stopPublish(); } } //停止发布(录制) private function stopPublish(): void { if ( this ._nsPublish != null ) { this ._nsPublish.removeEventListener(NetStatusEvent.NET_STATUS, nsPublishNetStatus); this ._nsPublish.close(); this ._nsPublish = null ; } this ._btnRecord. label = "录制" ; this ._lblResult.text = "" ; this ._btnRecord.removeEventListener(MouseEvent.CLICK, prepareStopRecord); this ._btnRecord.addEventListener(MouseEvent.CLICK, startRecord); //允许回放; this ._btnPlay.enabled = true ; this ._btnPlay. label = "播放" ; this ._btnPlay.removeEventListener(MouseEvent.CLICK,doStopPlay); this ._btnPlay.addEventListener(MouseEvent.CLICK,doPlay); } //等待录制视频缓冲区的数据全部保存到服务器上; private function doWait(e:TimerEvent) { var _bufferLength = this ._nsPublish.bufferLength; if (_bufferLength <= 0 ) { _timer.removeEventListener(TimerEvent.TIMER, doWait); _timer.stop(); _timer = null ; stopPublish(); } } function checkCameraComplete(e:TimerEvent): void { this ._lblResult.text = "设备无法使用(有可能被占用)" ; _timer.removeEventListener(TimerEvent.TIMER, checkCamera); _timer.removeEventListener(TimerEvent.TIMER_COMPLETE, checkCameraComplete); _timer = null ; } } } |
界面:
示例源代码下载:http://cid-2959920b8267aaca.office.live.com/self.aspx/Flash/VideoRecording.rar
作者:菩提树下的杨过
出处:http://yjmyzz.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
出处:http://yjmyzz.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
2008-09-04 WCF Testing Tool(转)
2008-09-04 [转自雨痕]RESTful WCF
2008-09-04 [转]用HTTP/Post与WCF互操作
2008-09-04 [转载]利用SQL SERVER2005发送邮件
2008-09-04 [转贴]三种Ext提交数据的方法
2008-09-04 ExtJs学习笔记(20)-利用ExtJs的Ajax与服务端WCF交互