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目录即可。


相关文章

  译序:JWMediaPlayer是开源的网页使用的Flash播放器。本...
    Flash编程原理都是只能将1写为0,而不能将0写成1.所...
 上传setenvgatewayip192.168.1.1;setenvserverip192.168.1...
Error:FlashDownloadFailed-"Cortex-M3"出现一般有...
jPlayer是一个用于控制和播放mp3文件的jQuery插件。它在后台...
#ifndef__FONTUPD_H__#define__FONTUPD_H__#include"sy...