flash + FMS 发布 录制 接受 h264视频

发布端

<?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"
      width="800" height="600"
     
      creationComplete="init()">
 <fx:Declarations>
  <!-- 将非可视元素(例如服务、值对象)放在此处 -->
 </fx:Declarations>
 <fx:Script>
  <![CDATA[
   
   private var nc:NetConnection;
   
   private var ns:NetStream;
   
   private var camera:Camera;
   private var microphone:Microphone;
   private var video:Video;
   
   private var h264:H264VideoStreamSettings = new H264VideoStreamSettings();
   
   private function init():void
   {
    initCameraAndMicrophone();
   }
   
   private function initNet():void
   {
    
    nc = new NetConnection();
    nc.connect("rtmp://localhost/testH264");
    nc.addEventListener(NetStatusEvent.NET_STATUS,onStatusEvent);
    nc.client = this;
   }
   
   private function initCameraAndMicrophone():void
   {
    camera = Camera.getCamera();
    microphone = Microphone.getMicrophone();
    trace(camera);
    if(Camera.isSupported)
    {
     if(camera)
     {
      
      if(camera.muted)
      {
       camera.addEventListener(StatusEvent.STATUS,cameraStatusEventHandler);
       camera.addEventListener(ActivityEvent.ACTIVITY,activityEventHandler);
      }
      camera.setMode(320,240,15);
      video = new Video();
      video.smoothing = true;
      video.attachCamera(camera);
      this.videoDisplay.addChild(video);
     }
     else
     {
      trace("No Camera is installed.")
     }
    }
   }
   
   private function onStatusEvent(event:NetStatusEvent):void
   {
    trace(event.info.code);
    var code:String = event.info.code;
    switch(code)
    {
     case "NetConnection.Connect.Success":
      creatStream();
      break;
    }
   }
   
   private function cameraStatusEventHandler(event:StatusEvent):void
   {
    trace(event.code);
    switch(event.code){
     case "Camera.Muted":
      break;
     case "Camera.Unmuted":
      initNet();
      break;
    }
   }
   
   private function activityEventHandler(event:ActivityEvent):void
   {
     
   }
   
   private function creatStream():void
   {
    ns = new NetStream(nc);
    ns.addEventListener(NetStatusEvent.NET_STATUS,onStatusEvent);
    var h264Stream:H264VideoStreamSettings = new H264VideoStreamSettings();
    h264Stream.setProfileLevel(H264Profile.BASELINE,H264Level.LEVEL_5_1);
    h264Stream.setMode(1280,720,25);
    h264Stream.setQuality(0,80);
    ns.videoStreamSettings = h264Stream;
    ns.attachCamera(camera);
    ns.attachAudio(microphone);
    ns.publish("mp4:testH264_1.mp4","record");
   }
   
   private function onMetaDataHandler(info:Object):void
   {
    
   }
   
  ]]>
 </fx:Script>
 <s:VideoDisplay id="videoDisplay" width="320" height="240" horizontalCenter="0"
     verticalCenter="0"/>
</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"
      width="800" height="600"
     
      creationComplete="init()">
 <fx:Declarations>
  <!-- 将非可视元素(例如服务、值对象)放在此处 -->
 </fx:Declarations>
 <fx:Script>
  <![CDATA[
   
   private var nc:NetConnection;
   
   private var ns:NetStream;
   
   private var camera:Camera;
   private var microphone:Microphone;
   private var video:Video;
   
   private var h264:H264VideoStreamSettings = new H264VideoStreamSettings();
   
   private function init():void
   {
    //initCameraAndMicrophone();
    initNet();
   }
   
   private function initNet():void
   {
    
    nc = new NetConnection();
    nc.connect("rtmp://localhost/testH264");
    nc.addEventListener(NetStatusEvent.NET_STATUS,onStatusEvent);
    nc.client = this;
   }
   
   
   private function onStatusEvent(event:NetStatusEvent):void
   {
    trace(event.info.code);
    var code:String = event.info.code;
    switch(code)
    {
     case "NetConnection.Connect.Success":
      creatStream();
      break;
    }
   }
   
   private function creatStream():void
   {
    var nsClient:Object = {};
    nsClient.onMetaData = onMetaDataHandler;
    
    ns = new NetStream(nc);
    ns.client = nsClient;
    
    ns.addEventListener(NetStatusEvent.NET_STATUS,onStatusEvent);
       
    ns.play("mp4:testH264_1.mp4");
    video = new Video(320,240);
    video.attachNetStream(ns);
    this.videoDisplay.addChild(video);
   }
   
   private function onMetaDataHandler(info:Object):void
   {
    trace(info.duration);
   }
   
  ]]>
 </fx:Script>
 <s:VideoDisplay id="videoDisplay" width="320" height="240" horizontalCenter="0"
     verticalCenter="0"/>
</s:Application>

 

FMS服务器最好是4.5版本,

在应用服务器里面(application目录)新建一个testH264目录即可。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值