flex 聲音的錄製



<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" height="312" width="328" creationComplete="init()">
<mx:Script>
<![CDATA[
//預定義聲明
import mx.controls.Alert;
import mx.events.SliderEvent;

//創建一個基本的網絡連接對象
private var vi:Video;
private var cam:Camera;      //定義一個攝像頭
private var inNs:NetStream;
private var outNs:NetStream;
private var nc:NetConnection;
private var mic:Microphone; //定義一個麥克風

private var _duration:Number;            //視頻播放時間
private var playPosition:Number;         //定義播放進度位置
private var soundPosition:Number;    //定義聲音大小控制條的位置
private var flag:Boolean = false;      
private var lastVideoName:String = "";   //視頻錄製後保存的名字
private var _url:String = "rtmp://localhost/oflaDemo";

public function init():void{
setupCamera();    //初始化攝像頭信息
}
//開始錄製按扭點擊
public function clickConnect():void{
nc = new NetConnection();
nc.client = this ;
nc.addEventListener(NetStatusEvent.NET_STATUS,nsHandler);
nc.connect(_url);         //連接red5服務器
}
public function nsHandler(evt:NetStatusEvent):void{
if (evt.info.code == "NetConnection.Connect.Success"){           //如果連接成功
playClick();
}else{
Alert.show("連接失敗");  
}  
}
//開始錄製
public function playClick():void{ 
if(vi != null){
vi.clear();
vdisplay.removeChild(vi);
vi = new Video();
vi.width = 320;
vi.height = 240;
vi.attachCamera(cam);
vdisplay.addChild(vi);
}
outNs = new NetStream(nc);     
outNs.attachCamera(cam);   //把攝像頭存入outNs
outNs.attachAudio(mic);          //把麥克風存入outNs
lastVideoName = "red5RecordDemo_" + Math.random()+getTimer();
outNs.publish(lastVideoName, "record");

startRec.enabled = false;
stopRec.enabled = true; 
}
//停止錄製
public function stopClick():void{
//關閉ns與red5的連接
outNs.close();
vi.clear();
vdisplay.removeChild(vi);
//鎖定開始按鍵使其生效
startRec.enabled = true;
//鎖定停止按鍵使其失效
stopRec.enabled = false;
}
//錄製完以後播放
public function playLastVideo():void{
if(nc!=null){
addEventListener(Event.ENTER_FRAME,onEnterFrame);
inNs = new NetStream(nc);
//定義onMetaData,獲取視頻相關數據
var customClient:Object = new Object();
customClient.onMetaData = function(metadata:Object):void{
_duration = metadata.duration; //獲取視頻持續時間
t_sh.maximum = _duration;
}
inNs.client = customClient;
//刪除原_localVideo,便於在錄製和播放視頻之間切換
vi = new Video();
vi.width = 320;
vi.height = 240;
vi.attachNetStream(inNs);
vdisplay.addChild(vi);
inNs.play(lastVideoName+".flv");
addEventListener(Event.ENTER_FRAME,onEnterFrame);
}
}

public function onBWDone():void {
}

//初始化攝像頭
public function setupCamera():void{
//啓動攝像頭
cam = Camera.getCamera();
if(cam != null){
cam.addEventListener(StatusEvent.STATUS,onStatusHandler);
cam.addEventListener(ActivityEvent.ACTIVITY,onActiveHandler);
cam.setMode(320,240,30);
cam.setQuality(0,70);  //設置清晰度
vi = new Video();
vi.width = 320;
vi.height = 240;
vi.attachCamera(cam);
vdisplay.addChild(vi);

}
mic = Microphone.getMicrophone();
if(mic != null){
mic.setSilenceLevel(0,-1); //設置麥克風保持活動狀態並持續接收集音頻數據
mic.gain = 80; //設置麥克風聲音大小
}
}

private function onActiveHandler(event:ActivityEvent):void
{
if(!cam.muted){   //判斷攝像頭存不存在
startRec.enabled = true;
}else{
Alert.show("錯誤:無法鏈接到活動攝像頭!")
}
cam.removeEventListener(StatusEvent.STATUS,onStatusHandler);
}

private function onStatusHandler(event:StatusEvent):void{
if(!cam.muted){   //判斷攝像頭存不存在
startRec.enabled = true;
}else{
Alert.show("錯誤:無法鏈接到活動攝像頭!")
}
cam.removeEventListener(StatusEvent.STATUS,onStatusHandler);
}

public function thumbPress(event:SliderEvent):void{
inNs.togglePause();
removeEventListener(Event.ENTER_FRAME,onEnterFrame);
}
private function thumbChanges(event:SliderEvent):void{
playPosition = t_sh.value;        //當前播放視頻進度的位置=當前播放進度條的位置
inNs.seek(playPosition);
addEventListener(Event.ENTER_FRAME,onEnterFrame);
}
private function thumbRelease(event:SliderEvent):void{      //釋放mouse後執行
inNs.seek(playPosition);                             //查找當前進度條位置
inNs.togglePause();
addEventListener(Event.ENTER_FRAME,onEnterFrame);
}
public function onEnterFrame(event:Event):void{
if(_duration > 0 && inNs.time > 0){ //如果視頻時間和正在播放視頻的時間大於0
t_sh.value =inNs.time;
lbtime.text = formatTimes(inNs.time) + " / "+ formatTimes(_duration);

if(formatTimes(inNs.time)==formatTimes(_duration)){    //如果播放完畢,則關畢流,初始化擺放時間的label
if(flag==true){        //如果是加載,就不執行 ||false代表是加載,true代表是播放結束
removeEventListener(Event.ENTER_FRAME,onEnterFrame);
inNs.close();  
lbtime.text = "0:00 / "+ formatTimes(_duration);
}
setTimeout(function():void{flag = true;},1000);
}
}
//時間格式操作
private function formatTimes(value:int):String{
var result:String = (value % 60).toString();
if (result.length == 1){
result = Math.floor(value / 60).toString() + ":0" + result;
} else {
result = Math.floor(value / 60).toString() + ":" + result;
}
return result;
}
//聲音音量控制
private function sound_thumbChanges(event:SliderEvent):void{
soundPosition = th_sound.value;
}
private function sound_thumbRelease(event:SliderEvent):void{
vdisplay.volume = soundPosition;
}

]]>
</mx:Script>
<mx:VideoDisplay x="0" y="0" width="324.5" height="240" id="vdisplay"/>
<mx:Button x="10" y="250" label="開始錄製" id="startRec" click="clickConnect()" enabled="false" />
<mx:Button x="10" y="280" label="停止錄製" width="70" id="stopRec" click="stopClick()" enabled="false" />
<mx:Button x="253" y="268" label="播放" click="playLastVideo()"  />
<mx:HSlider x="98" y="248" width="143" id="t_sh" thumbPress="thumbPress(event)" thumbRelease="thumbRelease(event)" change="thumbChanges(event)"/>
<mx:Label x="237" y="242" text="0:00/0:00" width="89" textAlign="center" height="18" id="lbtime"/>
<mx:HSlider x="98" y="278" width="91" id="th_sound" minimum="0" maximum="1" value="{vdisplay.volume}" change="sound_thumbChanges(event)" thumbRelease="sound_thumbRelease(event)"/>
<mx:Label x="187" y="270" text="sound" height="20" width="44" textAlign="center"/>
</mx:Application>

分享到:

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章