FMS3系列(四)在線視頻錄制、視頻回放
FMS3系列(四):在線視頻錄制、視頻回放 使用Flash/Flex+FMS實(shí)現(xiàn)在線視頻錄制、視頻回放的很簡單的。通過閱讀API文檔后基本都可以實(shí)現(xiàn)這個(gè)功能,本文也意在拋磚引玉,希望對剛?cè)胧诌@塊的朋友有所幫助。 首先建立好Flash(ActionScript 3.0)文件,從組件(可使用Ctrl+F7打開)庫中拖拽相應(yīng)的組件到Flash舞臺上,如下圖: 界面布局好后我們通過程序設(shè)置組見的顯示文本以及為按扭添加事件監(jiān)聽,新建一個(gè)ActionScript類文件,編寫代碼如下: 1 public function PublishPlay():void 2 3 lbName.text="請輸入視頻文件名:" 4 btnPublish.label="開始錄制" 5 btnPublish.addEventListener(MouseEvent.CLICK,onPublishClick); 6 btnStop.label="停止錄制" 7 btnStop.addEventListener(MouseEvent.CLICK,onStopHandler); 8 btnPlay.label="視頻回放" 9 btnPlay.addEventListener(MouseEvent.CLICK,onPlayHandler);10 11 video=new Video();12 cam = Camera.getCamera();13 mic = Microphone.getMicrophone();14 if(cam=null)15 16 trace("沒檢測到視頻攝像頭");17 18 else19 20 video.attachCamera(cam);21 22 addChild(video);23 以上代碼同時(shí)實(shí)現(xiàn)了將視頻顯示到flash界面上,通過Camera的靜態(tài)方法getCamrea()方法可以直接獲取到視頻攝像頭的數(shù)據(jù)。其中用到的video,cam和mic變量為預(yù)先定義好的,如下:1 private var nc:NetConnection;2 private var ns:NetStream;3 private var video:Video;4 private var cam:Camera;5 private var mic:Microphone; 接下來就需要連接到FMS服務(wù)器實(shí)現(xiàn)視頻錄制功能了,通過NetConnection類實(shí)現(xiàn)與FMS服務(wù)器的連接,并通過流將視頻數(shù)據(jù)發(fā)布到FMS服務(wù)器。 1 private function onPublishClick(evt:MouseEvent):void 2 3 nc=new NetConnection(); 4 nc.addEventListener(NetStatusEvent.NET_STATUS,onPublishStatusHandler); 5 nc.connect("rtmp:/localhost/PulishedStreams"); 6 7 8 private function onPublishStatusHandler(evt:NetStatusEvent):void 9 10 if(evt.info.code="NetConnection.Connect.Success")11 12 ns=new NetStream(nc);13 ns.addEventListener(NetStatusEvent.NET_STATUS,onPublishStatusHandler);14 ns.client=new CustomClient();15 ns.attachCamera(cam);16 ns.attachAudio(mic);17 ns.publish(tbName.text,"record");18 19 在錄制視頻的時(shí)候視頻命名是取的文本輸入框的值作為視頻名,OK,現(xiàn)在測試Flash(Ctrl+Enter),通過點(diǎn)擊 按扭開始錄制視頻。通過查看FMS服務(wù)器的文件目錄可以看到,剛剛測試錄制的視頻存放于FMS服務(wù)器應(yīng)用下的streams/_definst_目錄下。詳見下圖所示: 錄制功能完成了,通過測試也可以成功的錄制視頻。最后我們通過程序來播放剛剛錄制是視頻。關(guān)于播放視頻在上一篇文章FMS3系列(三):創(chuàng)建基于FMS的流媒體播放程序,看山寨幫的山寨傳奇中已介紹怎么實(shí)現(xiàn),這里就直接帖出代碼不做解釋。 1 private function onPlayHandler(evt:MouseEvent):void 2 3 nc=new NetConnection(); 4 nc.addEventListener(NetStatusEvent.NET_STATUS,onPlayStatusHandler); 5 nc.connect("rtmp:/localhost/PulishedStreams"); 6 7 8 private function onPlayStatusHandler(evt:NetStatusEvent):void 9 10 if(evt.info.code="NetConnection.Connect.Success")11 12 ns=new NetStream(nc);13 ns.addEventListener(NetStatusEvent.NET_STATUS,onPlayStatusHandler);14 ns.client=new CustomClient();15 16 video = new Video();17 video.attachNetStream(ns);18 ns.play(tbName.text,0);19 addChild(video);20 21 通過本文的基礎(chǔ)上可以很方便的擴(kuò)展出在線拍照等多種應(yīng)用,有興趣的朋友可以去試驗(yàn)下。下面是本文完整的示例代碼。本文完整示例代碼 1package 2 3 import .*; 4 import flash.events.*; 5 import flash.media.*; 6 import flash.display.*; 7 import fl.controls.*; 8 9 public class PublishPlay extends Sprite10 11 private var nc:NetConnection;12 private var ns:NetStream;13 private var video:Video;14 private var cam:Camera;15 private var mic:Microphone;16 17 public function PublishPlay():void18 19 lbName.text="請輸入視頻文件名:"20 btnPublish.label="開始錄制"21 btnPublish.addEventListener(MouseEvent.CLICK,onPublishClick);22 btnStop.label="停止錄制"23 btnStop.addEventListener(MouseEvent.CLICK,onStopHandler);24 btnPlay.label="視頻回放"25 btnPlay.addEventListener(MouseEvent.CLICK,onPlayHandler);26 27 video=new Video();28 cam = Camera.getCamera();29 mic = Microphone.getMicrophone();30 if(cam=null)31 32 trace("沒檢測到視頻攝像頭");33 34 else35 36 video.attachCamera(cam);37 38 addChild(video);39 40 41 private function onStatusHandler(evt:NetStatusEvent):void42 43 trace(evt.info.code);44 if(evt.info.code="NetConnection.Connect.Success")45 46 ns=new NetStream(nc);47 ns.addEventListener(NetStatusEvent.NET_STATUS,onStatusHandler);48 ns.client=new CustomClient();49 50 51 52 private function onPublishClick(evt:MouseEvent):void53 54 nc=new NetConnection();55 nc.addEventListener(NetStatusEvent.NET_STATUS,onPublishStatusHandler);56 nc.connect("rtmp:/localhost/PulishedStreams");57 58 59 private function onPublishStatusHandler(evt:NetStatusEvent):void60 61 if(evt.info.code="NetConnection.Connect.Success")62 63 ns=new NetStream(nc);64 ns.addEventListener(NetStatusEvent.NET_STATUS,onPublishStatusHandler);65 ns.client=new CustomClient();66 ns.attachCamera(cam);67 ns.attachAudio(mic);68 ns.publish(tbName.text,"record");69 70 71 72 private function onStopHandler(evt:MouseEvent):void73 74 nc.close();75 76 77 private function onPlayHandler(evt:MouseEvent):void78 79 nc=new NetConnection();80 nc.addEventListener(NetStatusEvent.NET_STATUS,onPlayStatusHandler);81 nc.connect("rtmp:/localhost/PulishedStreams");82 83 84 private function onPlayStatusHandler(evt:NetStatusEvent):void85 86 if(evt.info.code="NetConnection.Connect.Success")87 88 ns=new NetStream(nc);89 ns.addEventListener(NetStatusEvent.NET_STATUS,onPlayStatusHandler);90 ns.client=new CustomClient();91 92 video = new Video();93 video.attachNetStream(ns);94 ns.play(tbName.text,0);95 addChild(video);96 97 98 99