[原]用RED5做直播

服务端用Java web或red5 server即可,直播和收看都是用Flash Builder 4做的flash。
先看效果图:
[原]用RED5做直播_第1张图片
[原]用RED5做直播_第2张图片

red5最新版已经更新到1.0.5 ,需要JDK8的支持。RED5主页: https://github.com/Red5

服务器端用Java做web工程,主类继续ApplicationAdapter 空实现即可。
当然你也可以不用做java工程,在 https://github.com/Red5/red5-server
下载和你本机对应的版本,一般0.8以后即可,最新是1.0.5。需要注意的1.0.5必须安装JDK8来支持。

  package org;

import org.red5.server.adapter.ApplicationAdapter;
import org.red5.server.api.IClient;
import org.red5.server.api.IConnection;
import org.red5.server.api.IScope;

public class Application extends ApplicationAdapter {

    @Override
    public synchronized boolean connect(IConnection conn, IScope scope,
            Object[] params)
    {
        System.out.println("新连接......."+conn.isConnected()+"........."+params.length);
        return super.connect(conn, scope, params);
    }


    @Override
    public synchronized boolean join(IClient client, IScope scope)
    {
        return super.join(client, scope);
    }

    @Override
    public synchronized void leave(IClient client, IScope scope)
    {
        super.leave(client, scope);
    }

    @Override
    public synchronized boolean start(IScope scope)
    {
        // TODO Auto-generated method stub
        return super.start(scope);
    }

    @Override
    public synchronized void stop(IScope scope)
    {
        super.stop(scope);
    }

    @Override
    public void disconnect(IConnection conn, IScope scope) {
        System.out.println("断开连接......."+conn.isConnected());
    }

}

下面是Flex代码:
发布直播端:

  <?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" creationComplete="init()"  >
    <fx:Script>
        <![CDATA[
            import mx.controls.Alert;
            private var nc:NetConnection ;
            private var cam:Camera ;
            private var mic:Microphone;
            private var ns:NetStream;

            /***
             *** 初始化的方法,默认显示摄像头实时图像,不发布和录像
             * ***/
            private function init():void
            {
                cam = Camera.getCamera();
                var vi:Video = new Video();
                vi.width = 313;
                vi.height = 194;
                vi.attachCamera(cam);
                videoDisplay.addChild(vi);
            }
            /***
             *** 发布直播并录制的方法 
             ***/           
            private function playClick():void
            {
                playButton.enabled = false;
                stopButton.enabled = true;
                recordCheck.enabled = false;
                //之前有连接,先关闭
                try{ 
                    if(nc.connected){ 
                        nc.close(); 
                    } 
                }catch(e:Error){} 
                nc = new NetConnection();
                nc.addEventListener(NetStatusEvent.NET_STATUS,connectHandler);
                nc.client  = this;
                //实例名称
                nc.connect("rtmp://localhost/player");
            }

            private function connectHandler(evt:NetStatusEvent):void{
                trace(evt.info.code);
                //由于flash的异步机制,连接成功后才能做处理,否则NetStream会因为conn没能连接报参数错误#2126
                if (evt.info.code == 'NetConnection.Connect.Success') {
                    ns = new NetStream(nc);
                    cam = Camera.getCamera();
                    if(cam == null)
                    {
                        Alert.show("没有发现摄像头","提示")
                    }
                    mic = Microphone.getMicrophone();

                    setupCameraMic();
                    ns.attachAudio(mic);
                    ns.attachCamera(cam);

                    var vi:Video = new Video();
                    vi.width = videoDisplay.width;
                    vi.height = videoDisplay.height;
                    vi.attachCamera(cam);
                    videoDisplay.addChild(vi);
                    // live 直播不录制, record 边直播边录制
                    ns.publish("ok", recordCheck.selected?"record":"live");
                }
                else
                {
                    Alert.show("直播服务器繁忙,请稍候再试","提示")
                }
            }
            /***
             ***设置摄像头和麦克风的参数
             ***/           
            private function setupCameraMic():void
            {
                cam = Camera.getCamera();
                cam.setMode(320, 240, 30);
                cam.setQuality(0,70);
                mic = Microphone.getMicrophone();

            }

            /***
             ***停止直播
             ***/
            private function stopClick():void
            {
                nc.close();
                ns.close();
                playButton.enabled = true;
                stopButton.enabled = false;
                recordCheck.enabled = true;
            }


        ]]>
    </fx:Script>
    <fx:Declarations></fx:Declarations>
    <s:Panel x="66" y="29" title="发布直播" width="534" height="319">
        <s:VideoDisplay  x="6" y="10" width="370" height="262" id="videoDisplay" />
        <s:Button label="开始" id="playButton" click="playClick();" x="384" y="24"/>
        <s:Button label="停止" x="384" y="68"  id="stopButton" click="stopClick();"/>
        <s:CheckBox x="469" y="25" label="录制" width="53" id="recordCheck"/>
    </s:Panel>
</s:Application>

收看直播端:

  <?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" creationComplete="init()">
    <fx:Script>
        <![CDATA[
            import mx.controls.Alert;
            private var nc:NetConnection;
            private var ns:NetStream;

            private function init():void{
                playButton.enabled = false;
                stopButton.enabled = true
                //之前有连接,先关闭
                try{ 
                    if(nc.connected){ 
                        nc.close(); 
                    } 
                }catch(e:Error){} 
                nc = new NetConnection();
                nc.client  = this;
                nc.addEventListener(NetStatusEvent.NET_STATUS,connectHandler);
                nc.connect("rtmp://localhost/player");
            }

            private function connectHandler(evt:NetStatusEvent):void{
                if (evt.info.code == 'NetConnection.Connect.Success') {
                    ns = new NetStream(nc);
                    var video:Video=new Video() ;
                    video.width = videoDisplay.width;
                    video.height = videoDisplay.height;
                    video.attachNetStream(ns);
                    //这里的名称要和服务端ns.publish方法中的名称一致
                    ns.play("ok");
                    videoDisplay.addChild(video);
                }
                else
                {
                    Alert.show("没有发现直播节目","提示"); 
                }
            }
            /***
             ***停止收看直播
             ***/
            private function stopClick():void
            {
                nc.close();
                ns.close();
                playButton.enabled = true;
                stopButton.enabled = false;
            }

        ]]>
    </fx:Script>
    <fx:Declarations>
    </fx:Declarations>
    <s:Panel x="132" y="78" width="496" height="257" title="收看现场直播" fontSize="14" >
        <s:VideoDisplay x="164" y="10" width="320" height="193" id="videoDisplay"/>
        <s:Button x="10" y="24" label="收看" id="playButton" click="init()"/>
        <s:Button x="10" y="75" label="停止"  id="stopButton" click="stopClick()" />
    </s:Panel>
</s:Application>

需要注意的
nc.connect(“rtmp://localhost/player”);
这里连接的是rtmp server的地址和实例,如果用我提供的附件的话实例名称 就是player,可以在root-web.xml中进行配置。如果你下载的是官方的server,实例名称是oflaDemo。这两处必须保持一致。

      <bean id="web.scope" class="org.red5.server.WebScope" init-method="register">
        <property name="server" ref="red5.server" />
        <property name="parent" ref="global.scope" />
        <property name="context" ref="web.context" />
        <property name="handler" ref="player.handler" />
        <property name="contextPath" value="/player" />
        <property name="virtualHosts" value="*,localhost, localhost:9449, 127.0.1.1:9449" />
    </bean>

当然,此示例你必须要有摄像头才能正常运行。

示例代码下载

作者:supercrsky 发表于2015/8/13 14:05:16 原文链接
阅读:38 评论:0 查看评论

你可能感兴趣的:([原]用RED5做直播)