《FMS3系列(四)在線視頻錄制、視頻回放》由會員分享,可在線閱讀,更多相關(guān)《FMS3系列(四)在線視頻錄制、視頻回放(9頁珍藏版)》請在裝配圖網(wǎng)上搜索。
1、FMS3系列(四):在線視頻錄制、視頻回放 使用Flash/Flex+FMS實現(xiàn)在線視頻錄制、視頻回放的很簡單的。通過閱讀API文檔后基本都可以實現(xiàn)這個功能,本文也意在拋磚引玉,希望對剛?cè)胧诌@塊的朋友有所幫助。首先建立好Flash(ActionScript 3.0)文件,從組件(可使用Ctrl+F7打開)庫中拖拽相應的組件到Flash舞臺上,如下圖:界面布局好后我們通過程序設置組見的顯示文本以及為按扭添加事件監(jiān)聽,新建一個ActionScript類文件,編寫代碼如下:1publicfunctionPublishPlay():void23lbName.text=請輸入視頻文件名:;4btnPub
2、lish.label=開始錄制;5btnPublish.addEventListener(MouseEvent.CLICK,onPublishClick);6btnStop.label=停止錄制;7btnStop.addEventListener(MouseEvent.CLICK,onStopHandler);8btnPlay.label=視頻回放;9btnPlay.addEventListener(MouseEvent.CLICK,onPlayHandler);1011video=newVideo();12cam=Camera.getCamera();13mic=Microphone.get
3、Microphone();14if(cam=null)1516trace(沒檢測到視頻攝像頭);1718else1920video.attachCamera(cam);2122addChild(video);23以上代碼同時實現(xiàn)了將視頻顯示到flash界面上,通過Camera的靜態(tài)方法getCamrea()方法可以直接獲取到視頻攝像頭的數(shù)據(jù)。其中用到的video,cam和mic變量為預先定義好的,如下:1privatevarnc:NetConnection;2privatevarns:NetStream;3privatevarvideo:Video;4privatevarcam:Camera;
4、5privatevarmic:Microphone;接下來就需要連接到FMS服務器實現(xiàn)視頻錄制功能了,通過NetConnection類實現(xiàn)與FMS服務器的連接,并通過流將視頻數(shù)據(jù)發(fā)布到FMS服務器。1privatefunctiononPublishClick(evt:MouseEvent):void23nc=newNetConnection();4nc.addEventListener(NetStatusEvent.NET_STATUS,onPublishStatusHandler);5nc.connect(rtmp:/localhost/PulishedStreams);678private
5、functiononPublishStatusHandler(evt:NetStatusEvent):void910if(evt.info.code=NetConnection.Connect.Success)1112ns=newNetStream(nc);13ns.addEventListener(NetStatusEvent.NET_STATUS,onPublishStatusHandler);14ns.client=newCustomClient();15ns.attachCamera(cam);16ns.attachAudio(mic);17ns.publish(tbName.text
6、,record);1819在錄制視頻的時候視頻命名是取的文本輸入框的值作為視頻名,OK,現(xiàn)在測試Flash(Ctrl+Enter),通過點擊 按扭開始錄制視頻。通過查看FMS服務器的文件目錄可以看到,剛剛測試錄制的視頻存放于FMS服務器應用下的streams/_definst_目錄下。詳見下圖所示:錄制功能完成了,通過測試也可以成功的錄制視頻。最后我們通過程序來播放剛剛錄制是視頻。關(guān)于播放視頻在上一篇文章FMS3系列(三):創(chuàng)建基于FMS的流媒體播放程序,看山寨幫的山寨傳奇中已介紹怎么實現(xiàn),這里就直接帖出代碼不做解釋。1privatefunctiononPlayHandler(evt:Mous
7、eEvent):void23nc=newNetConnection();4nc.addEventListener(NetStatusEvent.NET_STATUS,onPlayStatusHandler);5nc.connect(rtmp:/localhost/PulishedStreams);678privatefunctiononPlayStatusHandler(evt:NetStatusEvent):void910if(evt.info.code=NetConnection.Connect.Success)1112ns=newNetStream(nc);13ns.addEventLi
8、stener(NetStatusEvent.NET_STATUS,onPlayStatusHandler);14ns.client=newCustomClient();1516video=newVideo();17video.attachNetStream(ns);18ns.play(tbName.text,0);19addChild(video);2021通過本文的基礎(chǔ)上可以很方便的擴展出在線拍照等多種應用,有興趣的朋友可以去試驗下。下面是本文完整的示例代碼。本文完整示例代碼1package23import.*;4importflash.events.*;5importflash.media
9、.*;6importflash.display.*;7importfl.controls.*;89publicclassPublishPlayextendsSprite1011privatevarnc:NetConnection;12privatevarns:NetStream;13privatevarvideo:Video;14privatevarcam:Camera;15privatevarmic:Microphone;1617publicfunctionPublishPlay():void1819lbName.text=請輸入視頻文件名:;20btnPublish.label=開始錄制;
10、21btnPublish.addEventListener(MouseEvent.CLICK,onPublishClick);22btnStop.label=停止錄制;23btnStop.addEventListener(MouseEvent.CLICK,onStopHandler);24btnPlay.label=視頻回放;25btnPlay.addEventListener(MouseEvent.CLICK,onPlayHandler);2627video=newVideo();28cam=Camera.getCamera();29mic=Microphone.getMicrophone(
11、);30if(cam=null)3132trace(沒檢測到視頻攝像頭);3334else3536video.attachCamera(cam);3738addChild(video);394041privatefunctiononStatusHandler(evt:NetStatusEvent):void4243trace(evt.info.code);44if(evt.info.code=NetConnection.Connect.Success)4546ns=newNetStream(nc);47ns.addEventListener(NetStatusEvent.NET_STATUS,
12、onStatusHandler);48ns.client=newCustomClient();49505152privatefunctiononPublishClick(evt:MouseEvent):void5354nc=newNetConnection();55nc.addEventListener(NetStatusEvent.NET_STATUS,onPublishStatusHandler);56nc.connect(rtmp:/localhost/PulishedStreams);575859privatefunctiononPublishStatusHandler(evt:Net
13、StatusEvent):void6061if(evt.info.code=NetConnection.Connect.Success)6263ns=newNetStream(nc);64ns.addEventListener(NetStatusEvent.NET_STATUS,onPublishStatusHandler);65ns.client=newCustomClient();66ns.attachCamera(cam);67ns.attachAudio(mic);68ns.publish(tbName.text,record);69707172privatefunctiononSto
14、pHandler(evt:MouseEvent):void7374nc.close();757677privatefunctiononPlayHandler(evt:MouseEvent):void7879nc=newNetConnection();80nc.addEventListener(NetStatusEvent.NET_STATUS,onPlayStatusHandler);81nc.connect(rtmp:/localhost/PulishedStreams);828384privatefunctiononPlayStatusHandler(evt:NetStatusEvent):void8586if(evt.info.code=NetConnection.Connect.Success)8788ns=newNetStream(nc);89ns.addEventListener(NetStatusEvent.NET_STATUS,onPlayStatusHandler);90ns.client=newCustomClient();9192video=newVideo();93video.attachNetStream(ns);94ns.play(tbName.text,0);95addChild(video);96979899