环境:RED5 0.8 WIN 安装版 ECLIPSE GALILEO 版 ACTIONSCRIPT 3.0
项目名称:webcam
----------------------------------------------------------------------------------------------
一、配置文件
1、red5-web.properties
配置内容:
webapp.contextPath=/webcam
webapp.virtualHosts=localhost, 127.0.0.1
2、red5-web.xml
配置内容:
默认的配置内容只需要改动一处即可:
class="red5.Application"
singleton="true" />
注意:red5.Application 是我定义的应用类包路径,要与JAVA代码对应
3、web.xml
默认的配置内容只需要改动一下上下文名称即可
webAppRootKey
/webcam
注意:上下文名称是你的项目名也是发布路径
二、JAVA应用代码:
package red5;
import org.red5.server.adapter.MultiThreadedApplicationAdapter;
import org.red5.server.api.IConnection;
import org.red5.server.api.IScope;
import org.red5.server.api.Red5;
import org.red5.server.stream.ClientBroadcastStream;
public class Application extends MultiThreadedApplicationAdapter {
//create a instance for application
public Application app;
public IConnection conn;
public IScope scope;
public ClientBroadcastStream stream;//用来接受flash上传的stream的类
//start the recording
//客户端call这个function要传入流的名字
public String startRecord(String streamName){
//start a new recording
String fileName=String.valueOf(System.currentTimeMillis());
app=newApplication();
conn=Red5.getConnectionLocal();//得到当前的连接
scope=conn.getScope();//一组连入服务器的客户
app.connect(conn,scope,null);
System.out.println("connection Established!");
//注意stream name,在flash端也需要匹配
stream= (ClientBroadcastStream)app.getBroadcastStream(scope,streamName);
System.out.println("The publisher's name is: "+stream.getPublishedName()+", created at: "+stream.getCreationTime());
System.out.println("the stream Name is: "+fileName);
try{
stream.saveAs(fileName, false);
}catch (Exception e){
System.out.println(e.toString());
}
return fileName;
}
//stop the recording
public String stopRecord(){
stream.stopRecording();//CuPlayer.com停止记录
System.out.println("byte Recieved: "+stream.getBytesReceived());
System.out.println("Recording Stopped!");
return "Server stop recording!";
}
}
三、Actionscript 3.0 代码
import flash.display.MovieClip;
import flash.events.*;
import flash.media.Camera;
import flash.media.Microphone;
import flash.media.Video;
import flash.media.SoundCodec;
import flash.net.NetStream;
import flash.net.NetConnection;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
var resp:Responder=newResponder(onResult);
var _video:Video;
var _cam:Camera;
var _mic:Microphone;
var _nc:NetConnection;
var _ns:NetStream;
stage.align=StageAlign.TOP_LEFT;
stage.scaleMode=StageScaleMode.NO_SCALE;
createChildren();
initConn();
function createChildren():void {
_cam=Camera.getCamera();
_cam.setQuality(144000, 85);
_cam.setMode(320, 240, 15);
_cam.setKeyFrameInterval(60);
_video=newVideo();
_video.attachCamera(_cam);
addChild(_video);
_mic=Microphone.getMicrophone();
if(_mic != null){
_mic.setSilenceLevel(0,-1);
_mic.gain=80;
_mic.setLoopBack(true);
}
}
function initConn():void{
_nc=newNetConnection();
_nc.objectEncoding=ObjectEncoding.AMF3;
_nc.client=this;
_nc.addEventListener(NetStatusEvent.NET_STATUS , netStatus);
_nc.connect("rtmp://localhost/webcam/",true);
}
function publish():void {
if(_nc.connected){
_ns=newNetStream(_nc);
_ns.addEventListener(NetStatusEvent.NET_STATUS , netStatus);
_ns.attachCamera(_cam);
_ns.attachAudio(_mic);
_ns.publish("mystream", "live");
}
}
function netStatus (event:NetStatusEvent):void{
if ( event.info.code== "NetConnection.Connect.Success"){
publish();
}
}
function onResult(obj:Object):void{
;
}
b_start.addEventListener(MouseEvent.CLICK,btStart);
function btStart(Event:MouseEvent){
_nc.call("startRecord",new Responder(getInfor,onState),"mystream");
}
function getInfor(reobj:Object):void {
trace("Server returning Infor: "+reobj);
}
function onState(err:Object):void {
trace("Connection result error: "+err);
}
b_stop.addEventListener(MouseEvent.CLICK,btStop);
function btStop(Event:MouseEvent){
_nc.call("stopRecord",new Responder(getInfor,onState));
}
注意:FLASH只有一桢,b_start 与b_stop是我用MC做的一个按钮的实例名称。一个开始录制一个停止录制.
以上,即是全部的在线录制音\视频代码。