使用red5录像
Red5是一个采用Java开发开源的Flash流媒体服务器。它支持:把音频(MP3)和视频(FLV)转换成播放流; 录制客户端播放流(只支持FLV);共享对象;现场直播流发布;远程调用。Red5使用RSTP作为流媒体传输协议,在其自带的一些示例中演示了在线录 制,flash流媒体播放,在线聊天,视频会议等一些基本功能。
Red5 is an Open Source Flash Server written in Java that supports:
- Streaming Audio/Video (FLV and MP3 )
- Recording Client Streams (FLV only)
- Shared Objects
- Live Stream Publishing
- Remoting (AMF )
到http://www.red5.org/red5-server/下载最新的red5版本(我下的是linux版),只需要解压缩unzip -o red5-1.0.0-RC1.zip,然后执行./red5.sh即可将red5的服务启动起来。
访问http://localhost:5080,然后安装相应的服务,之后就可以查看demo了。
新建一web项目TestRed5,然后将WebRoot部分拷贝直red5的webapps目录下,补充好相应red5-web.properties和red5-web.xml还有web.xml文件,可以直接拷贝oflaDemo中的三个文件。然后重启red5服务,就可以访问TestRed5提供的red5服务了。
使用red5+flash提供视频录像和播放录像的主要功能是由red5提供录和播放的功能,使用flex制作一个flash
代码如下:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
creationComplete="init()">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
private var nc:NetConnection = null;
private var ns:NetStream = null;
private var video:Video = null;
private var camera:Camera = null;//定义一个摄像头
private var mic:Microphone; //定义一个麦克风
private var rtmp_url:String = "";// "rtmp://192.168.61.113/TestRed5";
private var videoName:String = ""; //视频保存名字
private function init():void{
//nc = new NetConnection();
setupCameraAndMic();
}
private function setupCameraAndMic():void{
camera = Camera.getCamera();
if(camera != null){
camera.addEventListener(StatusEvent.STATUS,cameraStatus);
camera.setMode(320,240,30);
camera.setQuality(0,70);
video = new Video();
video.width = 320;
video.height = 240;
video.attachCamera(camera);
videoPlay.addChild(video);
}
mic = Microphone.getMicrophone();
if(mic != null){
mic.addEventListener(StatusEvent.STATUS,micStatus);
mic.setSilenceLevel(0,-1); //设置麦克风保持活动状态并持续接收集音频数据
mic.gain = 80; //设置麦克风声音大小
}
}
private function cameraStatus(event:StatusEvent):void{
if(!camera.muted){
startRecord.enabled = true;
//error_label.text = error_label.text + "1";
}else{
error_label.text = error_label.text + " 无法找到摄像头"
}
//camera.removeEventListener(StatusEvent.STATUS,cameraStatus);
}
/* 好像麦克风没法检测,测试过拔掉麦克风,但是没效果 */
private function micStatus(event:StatusEvent):void{
if(!mic.muted){
startRecord.enabled = true;
//error_label.text = error_label.text + "2";
}else{
error_label.text = error_label.text + " 无法找到麦克风";
}
//mic.removeEventListener(StatusEvent.STATUS,micStatus);
}
/*点击 开始录像 按钮*/
private function clickStartRecord():void{
nc = new NetConnection();
nc.addEventListener(NetStatusEvent.NET_STATUS,nsHandler);
var name : String = root.loaderInfo.parameters.name; --从外界出入的参数
//rtmp_url = "rtmp://192.168.61.113/TestRed5";
rtmp_url = "rtmp://" + name + "/TestRed5";
error_label.text=rtmp_url + name;
//error_label.text=rtmp_url + name;
nc.connect(rtmp_url); //连接red5
//doStart();
}
private function nsHandler(evt:NetStatusEvent):void{
if(evt.info.code == "NetConnection.Connect.Success"){ //如果连接成功
doStart();
}else{
Alert.show("连接失败");
}
}
/*开始录像*/
private function doStart():void{
if(video != null){
video.clear();
videoPlay.removeChild(video);
video = new Video();
video.width = 320;
video.height = 240;
video.attachCamera(camera);
videoPlay.addChild(video);
}
ns = new NetStream(nc);
ns.attachCamera(camera);
ns.attachAudio(mic);
videoName = "vincent_"+Math.random()+getTimer();
ns.publish(videoName,"record");
startRecord.enabled = false;
stopRecord.enabled = true;
}
private function clickStopRecord():void{
ns.close();
video.clear();
videoPlay.removeChild(video);
startRecord.enabled = true;
stopRecord.enabled = false;
init();/*重新初始化Camera,否则Camera会默认使用完了,不再显示图像*/
}
]]>
</mx:Script>
<mx:Panel x="213" y="175" width="360.5" height="301" layout="absolute">
<mx:VideoDisplay x="10" y="10" width="320" height="240" id="videoPlay"/>
</mx:Panel>
<mx:Button x="270" y="498" label="开始录像" id="startRecord"
click="clickStartRecord()"/>
<mx:Button x="432" y="498" label="停止录像" id="stopRecord"
click="clickStopRecord()" enabled="false"/>
<!--
<mx:Button x="442" y="498" label="播放录像" id="playRecord" enabled="false"/>
-->
<mx:Label text="错误:" x="213" y="544" width="360.5" height="25"
id="error_label" fontSize="12"/>
</mx:Application>
将此mxml导出成videoRecorder2.swf文件,供web程序使用,此程序提供一个录像功能。
将videoRecorder2.swf嵌入web页面使用,将videoRecord2.swf导入项目中。
页面代码如下
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<html>
<head>
<title>视频录像</title>
</head>
<body>
<center>
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0"
width="400" height="350" id="myFlash">
<param name="movie" value="videoRecorder2.swf"/>
<param name="quality" value="high"/>
<param name="flashvars" value="name=192.168.61.113" /> --传参数给flash
<embed src="videoRecorder2.swf" quality="high" width="400" height="350" type="application/x-shockwave-flash"
pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"
name="myFlash" swLiveConnect="true"
flashvars="name=192.168.61.113"
/>
</object>
<br/>
<font color="red">温馨提示:在进行正式视频录像前,请先录制一小段视频<br/>以测试各种设备(特别是麦克风)是否能工作正常。</font>
</center>
</body>
</html>
启动相关服务,访问相关页面即可进行视频录像了。
很详细···很好哦···强力支持你