FMS3系列(三):創建基於FMS的流媒體播放程序,看山寨幫的山寨傳奇【轉】

本文主要介紹怎麼去創建基於FMS的流媒體播放程序,Flash客戶端通過網絡加載FMS服務器上的視頻流文件(.flv,.mp4等),實現視頻流的播放。

     要實現媒體流文件的播放是非常簡單的,只要在FMS服務器上提供好流媒體文件,Flash客戶端通過NetConnection連接到FMS服務器,然後通過NetStream加載就OK。關於怎麼連接FMS在本系列的前兩篇已有詳細介紹,首先得在fms上建立好服務器應用並部署好媒體文件,如下圖示:

           

 

     下面是在Flash中開發的流媒體文件播放示例程序:

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> 1 import flash.display.*;
 2 import flash.events.*;
 3 import flash.net.*;
 4 
 5 var nc:NetConnection = new NetConnection();
 6 var ns:NetStream;
 7 var video:Video;
 8 
 9 nc.connect("rtmp://localhost/PlayStreams");
10 nc.addEventListener(NetStatusEvent.NET_STATUS,onStatusHandler);
11 
12 function onStatusHandler(evt:NetStatusEvent):void
13 {
14     trace(evt.info.code);
15     if(evt.info.code=="NetConnection.Connect.Success")
16     {
17         ns=new NetStream(nc);
18         ns.addEventListener(NetStatusEvent.NET_STATUS,onStatusHandler);
19         ns.client=new CustomClient();
20         video=new Video();
21         video.attachNetStream(ns);
22         ns.play("2009031301",0);
23         addChild(video);
24     }
25 }


     看看上面的程序代碼是不是非常簡單,現在我對上面的代碼進行詳細的分析。程序從上到下思路很清晰,首先將程序中需要的相關包導入,然後定義了連接對象(NetConnection),流對象(NetStream)和視頻對象(Video)。

     通過NetConnection的connect方法連接到fms服務器(rtmp://localhost/PlayStreams),並添加網絡連接的事件處理函數,在此函數內判斷網絡連接狀態,如果連接成功(連接狀態:NetConnection.Connect.Success)則通過NetStream建立視頻流,調用NetStream的play方法播放指定的流媒體文件,然後將流附加到視頻對象並顯示在flash界面上。如下圖示:

          

     OK,我們已經實現了流媒體文件的播放,下面我們來擴展程序的功能,爲前面的視頻播放程序加上播放、暫停、停止以及重新播放等功能。這時可以在界面上放置幾個按扭來驅動這些功能,添加按扭代碼如下(當然也可以直接拖拽Botton組件):

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> 1 var btnPlay:Button=new Button();
 2 btnPlay.x=10;
 3 btnPlay.y=250;
 4 btnPlay.width=50;
 5 btnPlay.label="播放";
 6 btnPlay.addEventListener(MouseEvent.CLICK,onPlayHandler);
 7 addChild(btnPlay);
 8 
 9 var btnPause:Button=new Button();
10 btnPause.x=80;
11 btnPause.y=250;
12 btnPause.width=50;
13 btnPause.label="暫停";
14 btnPause.addEventListener(MouseEvent.CLICK,onPauseHandler);
15 addChild(btnPause);
16 
17 var btnStop:Button=new Button();
18 btnStop.x=150;
19 btnStop.y=250;
20 btnStop.width=50;
21 btnStop.label="停止";
22 btnStop.addEventListener(MouseEvent.CLICK,onStopHandler);
23 addChild(btnStop);
24 
25 var btnReplay:Button=new Button();
26 btnReplay.x=220;
27 btnReplay.y=250;
28 btnReplay.width=80;
29 btnReplay.label="重新播放";
30 btnReplay.addEventListener(MouseEvent.CLICK,onReplayHandler);
31 addChild(btnReplay);
32 
33 function onPlayHandler(evt:MouseEvent):void
34 {}
35 
36 function onPauseHandler(evt:MouseEvent):void
37 {}
38 
39 function onStopHandler(evt:MouseEvent):void
40 {}
41 
42 function onReplayHandler(evt:MouseEvent):void
43 {}

 

     這裏我們需要對上面的代碼進行一下重構,將流和控制視頻播放的代碼重構爲方法,以便在重新播放的時候直接調用。

 

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> 1 function playStream():void
 2 {
 3     ns=new NetStream(nc);
 4     ns.addEventListener(NetStatusEvent.NET_STATUS,onStatusHandler);
 5     ns.client=new CustomClient();
 6     video=new Video();
 7     video.attachNetStream(ns);
 8     ns.play("2009031302",0);
 9     addChild(video);
10 }

 

     上面我們已經將控制視頻播放、暫停、停止和重新播放的按扭構造在了flash界面上,現在只需要完成這些按扭的功能就是,要實現視頻的播放、暫停、停止和重新播放同樣是非常簡單的,NetStream爲我們提供了可直接調用的API。詳細如下:

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> 1 function onPlayHandler(evt:MouseEvent):void
 2 {
 3     ns.resume();
 4 }
 5 
 6 function onPauseHandler(evt:MouseEvent):void
 7 {
 8     ns.pause();
 9 }
10 
11 function onStopHandler(evt:MouseEvent):void
12 {
13     ns.close();
14 }
15 
16 function onReplayHandler(evt:MouseEvent):void
17 {
18     ns.close();
19     playStream();
20 }

 

     一切搞定 ,可以按下Ctrl+Enter測試了,看到了flash已經成功的加載到了fms上的視頻文件(.flv)。打開FMS管理控制檯就會看到,在應用"PlayStreams"下有一個NetStream的連接,代表當前應用有一個網絡流正在傳輸,如下圖:

 

完整示例代碼
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> 1import flash.display.*;
 2import flash.events.*;
 3import flash.net.*;
 4import fl.controls.*;
 5
 6var nc:NetConnection = new NetConnection();
 7var ns:NetStream;
 8var video:Video;
 9
10var btnPlay:Button=new Button();
11btnPlay.x=10;
12btnPlay.y=250;
13btnPlay.width=50;
14btnPlay.label="播放";
15btnPlay.addEventListener(MouseEvent.CLICK,onPlayHandler);
16addChild(btnPlay);
17
18var btnPause:Button=new Button();
19btnPause.x=80;
20btnPause.y=250;
21btnPause.width=50;
22btnPause.label="暫停";
23btnPause.addEventListener(MouseEvent.CLICK,onPauseHandler);
24addChild(btnPause);
25
26var btnStop:Button=new Button();
27btnStop.x=150;
28btnStop.y=250;
29btnStop.width=50;
30btnStop.label="停止";
31btnStop.addEventListener(MouseEvent.CLICK,onStopHandler);
32addChild(btnStop);
33
34var btnReplay:Button=new Button();
35btnReplay.x=220;
36btnReplay.y=250;
37btnReplay.width=80;
38btnReplay.label="重新播放";
39btnReplay.addEventListener(MouseEvent.CLICK,onReplayHandler);
40addChild(btnReplay);
41
42nc.connect("rtmp://192.168.1.103/PlayStreams");
43nc.addEventListener(NetStatusEvent.NET_STATUS,onStatusHandler);
44
45function onStatusHandler(evt:NetStatusEvent):void
46{
47    trace(evt.info.code);
48    if(evt.info.code=="NetConnection.Connect.Success")
49    {
50        playStream();
51    }

52}

53
54function playStream():void
55{
56    ns=new NetStream(nc);
57    ns.addEventListener(NetStatusEvent.NET_STATUS,onStatusHandler);
58    ns.client=new CustomClient();
59    video=new Video();
60    video.attachNetStream(ns);
61    ns.play("2009031302",0);
62    addChild(video);
63}

64
65function onPlayHandler(evt:MouseEvent):void
66{
67    ns.resume();
68}

69
70function onPauseHandler(evt:MouseEvent):void
71{
72    ns.pause();
73}

74
75function onStopHandler(evt:MouseEvent):void
76{
77    ns.close();
78}

79
80function onReplayHandler(evt:MouseEvent):void
81{
82    ns.close();
83    playStream();
84}

85
86

 

     如果在Flex環境下開發,更方便實現,詳細本文就不做介紹了,核心代碼和Flash裏開發是一樣的。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章