rtmp 点播系统之播放器篇

rtmp (Real Time Media Protocal) ,是实时流媒体协议,由Adobe公司提出,属于半开放的协议。此协议基于flash平台的音视频点播协议。

音视的点播系统分为两部分,分别为客户端和服务端。在本文中我会先讲解如何实现一个简单的rtmp播放器。至于rtmp服务器的实现,会在下文中分析。

1)首先是开发和调试环境的搭建(adobe flash builder + adobe flash player plugin debug + chrome)
1.1)下载安装adobe flash builder,并安装
1.2)下载并安装chrome
1.3)下载并安装 adobe flash player 调试版的插件(adobe flash player plubin debug), 
安装完后,由于chrome默认已安装了flash player的正式版,在些需要禁用默认安装的flash player,在chrome地址栏中输入chrome://plugins/会列出所有使用的插件,在些会发现有两个flash的插件,找到安装位置为“..../google/chrome/.."位置下的插件,并禁用它

2)现在我们已经搭好了开发环境,接下来,我们将用flash builder新建一个rtmp播放器的项目
2.1)打开flash builder,文件-》新建-》flex项目,弹出新建项目界面,设置一下项目名,项目位置以及应用程序类型,如下图所示:
rtmp 点播系统之播放器篇_第1张图片
接着直接点下一步,直到完成即可
rtmp 点播系统之播放器篇_第2张图片
rtmp 点播系统之播放器篇_第3张图片
2.2)新建ActionScript 类,类名为Rtmpplayer,包这里使用默认包
rtmp 点播系统之播放器篇_第4张图片
这个类主要是用来建立连接和netstream的建立
2.3)实现这个类的方法和接口,主要的思路:第一步建立连接
   var nc:NetConnection;
  nc = new NetConnection();
  nc.connect(stream_path); 
  并且监听这相新建连接的状态:
nc.addEventListener(NetStatusEvent.NET_STATUS,ConnectStatusHandle);
  当我们连接成功后,即收到
"NetConnection.Connect.Success"
 状态时,就可以创建网络流,并点播它
  完整的代码如下所示:
////////////////////////////////////////////////////////////
//rtmpplayer.as
package
{
        import  flash.events.NetStatusEvent;
        import  flash.events.StatusEvent;
        import  flash.media.Video;
        import  flash.net.NetConnection;
        import  flash.net.NetStream;
       
        public  class  RtmpPlayer  extends  Video
       {
               private  var  nc:NetConnection;
               private  var  ns:NetStream;
               public  var  stream_path:String = "rtmp://192.168.1.88:1935" ;
               public  var  stream_name:String =  "test.264" ;
               private  var  stream_client:Object =  new  Object();
              
               public  function  RtmpPlayer()
              {
                      super (width,height);
                      super .smoothing =  true ;
              }
               private  function  StartStream(name:String): void
              {
                     ns =  new  NetStream(nc);
                     ns.bufferTime = 0.3;
                     ns.client = stream_client;
                     ns.play(name);
                      super .attachNetStream(ns);
              }
               private  function  ConnectStatusHandle(event:NetStatusEvent): void
              {
                      switch (event.info.code)
                     {
                            case  "NetConnection.Connect.Success" :
                                   trace ( "currnet netconnection status: " +event.info.code);
                                  StartStream(stream_name);
                                   break ;
                            case  "NetConnection.Connect.Failed" :
                                   trace ( "currnet netconnection status: " +event.info.code);
                                   break ;
                            case  "NetConnection.Connect.Close" :
                                   trace ( "currnet netconnection status: " +event.info.code);
                                   break ;
                            default :
                                   break ;
                     }
              }
               public  function  SetPlayPath(path:String,name:String): void
              {
                     stream_path=path;
                     stream_name =name;
              }
               public  function  Connect(): void
              {
                      // new connection object
                     nc =  new  NetConnection();
                      //
                      //nc.client = this;
                      // connect to specific server
                     nc.connect(stream_path);   
                     nc.addEventListener(NetStatusEvent.NET_STATUS,ConnectStatusHandle);
              }
              
               public  function  TearDown(): void
              {
                      if (ns){
                           ns.close();
                           ns =  null ;
                     }
                      if (nc){
                            nc.removeEventListener(NetStatusEvent.NET_STATUS,ConnectStatusHandle);
                           nc.close();
                           nc = null ;
                     }
              }
       }
}
2.4) 设计界面,切换到rtmpplayer.mxml文件,并将源代码标签切到设计标签,从组建中拉出一个
VideoDisplay控件,用于显示图像;拖个文本框用于输入点播路径,最后再放两个按钮分别用于播放和
停止。新建控件时,我们要手动去设置一个控件的id,因为我们的控件操作的识别都是靠这个id号
界面效果如下图:
rtmp 点播系统之播放器篇_第5张图片

////////////////////////////////////////////////////////////////////////////
////rtmpplayer.mxml
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
                        xmlns:s="library://ns.adobe.com/flex/spark"
                        xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" xmlns:local="*"
                        creationComplete="Init(event)">

       <fx:Declarations>
              <!-- 将非可视元素(例如服务、值对象)放在此处 -->
              <local:Rtmpplayer id="rtmp_plu" width="{this.width}" height="{this.height}" />
       </fx:Declarations>   
       <fx:Script>
              <![CDATA[
                     import mx.events.FlexEvent;
                     
                     private function Init(event:FlexEvent):void
                     {
                           trace("create done\n");
                           //var rtmpdemo:RtmpPlayer = new RtmpPlayer();
                           //viewarea.addChild(rtmpdemo);
                           viewarea.addChild(rtmp_plu);
                            text_url.text=rtmp_plu.stream_path+"/"+rtmp_plu.stream_name;
                           //rtmp_plu.Connect();
                     }
                     private function PlayPathChange():void{
                           var url:String = text_url.text;
                           if(url.indexOf("rtmp://") == 0){
                                  var index:int= url.indexOf("/",7);
                                  if(index == -1){
                                         trace("invalid url");
                                  }else{
                                         rtmp_plu.stream_path= url.substring(0,index+1);
                                         rtmp_plu.stream_name=url.substring(index+1);
                                         trace("get stream path:"+rtmp_plu.stream_path + "    stram name:" + rtmp_plu.stream_name);
                                  }
                           }else{
                                  trace("invalid url");
                           }
                     }
              ]]>
       </fx:Script>
       <s:VideoDisplay id="viewarea" x="56" y="-4" width="640" height="480"/>
       <s:Label x="55" y="511" width="67" height="16" text="url"/>
       <s:TextInput id="text_url" x="130" y="510" width="460"text="rtmp://192.168.137.128:1935/test.flv" change="PlayPathChange()"/>
       <s:Button id="btn_play" x="55" y="549" label="play" click="rtmp_plu.Connect()"/>
       <s:Button id="btn_close" x="149" y="550" label="stop" click="rtmp_plu.TearDown()"/>
</s:Application>

3)调试运行。。。

你可能感兴趣的:(播放器,Flex,Flash,RTMP)