于FMS 的AS3录音机(转)

简介:

基本具备的功能,录音,暂停录音,重新录音,播放录音,暂停播放录音,以及停止播放。

代码如下:

核心录音

package com.DNight
{
import flash.events.EventDispatcher;
import flash.media.Microphone;
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.events.NetStatusEvent;
import flash.utils.setInterval;
import flash.utils.clearInterval;
import com.DNight.events.RecorderEvent
/**
* ...
* @author DN
*/
public class Recorder extends EventDispatcher
{
internal static const FMSSTR:String = "rtmp://localhost/FMSRecord";
public static const FMS_TOTAL_TIME:int = 10;
private var _netConnection:NetConnection;
private var _netStream:NetStream;
private var _mic:Microphone;

private var _recordRember:int = 0;
private var _recordName:String;
private var _recordingTime:int = 0;
private var _intervalId:uint;

public function get mic():Microphone {
return _mic
}
public function get recordName():String {
return _recordName;
}
public function Recorder()
{
_netConnection = new NetConnection();
_netConnection.connect(Recorder.FMSSTR);
_netConnection.addEventListener(NetStatusEvent.NET_STATUS, onNetStatusHandle);

_mic = Microphone.getMicrophone();
_mic.gain = 60;
_mic.rate = 11;
_mic.setUseEchoSuppression(true);
_mic.setLoopBack(true);
_mic.setSilenceLevel(5, 1000);
}

private function onNetStatusHandle(event:NetStatusEvent) {
switch(event.info.code) {
case "NetConnection.Connect.Success":
trace("连接成功");
_netStream = new NetStream(_netConnection);

break
case "NetConnection.Connect.Failed":
trace("连接失败");
break
}
}

public function startRecord() {
if (_recordRember == 0) {
//trace("第一次开始录制");
var nowTime:Date = new Date();
_recordName = nowTime.getTime().toString();
_netStream.attachAudio(_mic);
_netStream.publish(_recordName,"record");
_recordRember = 1;
}else {
//trace("继续录制");
_netStream.publish(_recordName,"append");
}
_intervalId=setInterval(upData,1000);
}

public function pauseRecord() {
//trace("暂停录制");
_netStream.close();
clearInterval(_intervalId);
}

public function stopRecord() {
//trace("停止录制,重新录制");
_netStream.close();
_recordRember = 0;
clearInterval(_intervalId);
_recordingTime = 0;
}

private function upData() {
_recordingTime++;
if (_recordingTime<=Recorder.FMS_TOTAL_TIME) {
var recordProgress:RecorderEvent = new RecorderEvent(RecorderEvent.RECORD_PROGRESS);
this.dispatchEvent(recordProgress);
}else {
var recordEnd:RecorderEvent = new RecorderEvent(RecorderEvent.RECORD_END);
this.dispatchEvent(recordEnd);
clearInterval(_intervalId);
_recordingTime = 0;
}
}
}

}

核心播放

package com.DNight
{
import adobe.utils.CustomActions;
import com.DNight.events.PlayEvent;
import flash.events.EventDispatcher;
import flash.media.Video;
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.events.NetStatusEvent;
import flash.media.SoundMixer
/**
* ...
* @author DN
*/
public class Player extends EventDispatcher
{
private var _path:String
private var _video:Video;
private var _netConnection:NetConnection;
private var _netStream:NetStream;
private var _duration:Number;
private var _time:Number = 0;

public function get duration() {
return _duration
}
public function get stream() {
return _netStream
}
public function Player(newPath)
{
_path = newPath;
_video = new Video();
_netConnection = new NetConnection();
_netConnection.connect(Recorder.FMSSTR);
_netConnection.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandle);
}

private function netStatusHandle(eve:NetStatusEvent) {
switch(eve.info.code) {
case "NetConnection.Connect.Success":
_netStream = new NetStream(_netConnection);
_netStream.bufferTime = 3;
var client:Object = new Object();
client.onMetaData = onMetaData;
client.onPlayStatus = onPlayStatus;
_netStream.client = client;
break
}
}

private function onMetaData(data:Object) {
_duration = data.duration;
var event:PlayEvent = new PlayEvent(PlayEvent.GET_DUREATON);
dispatchEvent(event);
//trace("音频长度"+_duration);
}

private function onPlayStatus(data:Object) {
switch(data.code) {
case "NetStream.Play.Complete":
var event:PlayEvent = new PlayEvent(PlayEvent.PLAY_END);
dispatchEvent(event);
_time = 0;
break
}
}

public function play() {
if (_time == 0) {
_netStream.play(_path);
}else {
_netStream.resume();
}
}

public function pause() {
_time = _netStream.time;
_netStream.pause();
}

public function stop() {
trace("停止音乐");
_netStream.close();
SoundMixer.stopAll();
}
}

}

管理(主类):

package com.DNight
{
import flash.display.Sprite;
import flash.events.Event
import flash.events.MouseEvent;
import com.DNight.events.RecorderEvent
import com.DNight.events.PlayEvent
import flash.media.Microphone;
import flash.net.NetStream;
/**
* ...
* @author DN
*/
public class RecordPlayer extends Sprite
{
private var _recording:Boolean;
private var _recorder:Recorder;
private var _mic:Microphone
private var _recordingTime:int = 0;
private var _line:Sprite
private var _data:Array;

private var _playing:Boolean
private var _player:Player;
private var _duration:Number = 0;
private var _nowTime:Number = 0;
private var _stream:NetStream
public function RecordPlayer()
{
this.addEventListener(Event.ADDED_TO_STAGE,onAddToStageHandle);
}

private function onAddToStageHandle(event:Event) {
this.recordBtn.addEventListener(MouseEvent.CLICK, onrecordBtnClickHandle);
resetBtn.gotoAndStop("notdo");
playBtn.gotoAndStop("notdo");
stopBtn.gotoAndStop("notdo");
_recorder = new Recorder();
_recorder.addEventListener(RecorderEvent.RECORD_PROGRESS, onRecorderProgressHandle);
_recorder.addEventListener(RecorderEvent.RECORD_END, onRecorderEndHandle);
_mic = _recorder.mic
}

private function onrecordBtnClickHandle(event:MouseEvent) {
_recording = !_recording;
if (_recording) {
recordBtn.gotoAndStop("pause");
resetBtn.gotoAndStop("cando");
playBtn.gotoAndStop("notdo");
resetBtn.addEventListener(MouseEvent.CLICK, onResetBtnClickHandle);
playBtn.removeEventListener(MouseEvent.CLICK, onPlayBtnClickHandle);
//开始录制
_recorder.startRecord();
_data = new Array(308);
this.addEventListener(Event.ENTER_FRAME, onReocrdingHandle);
//播放器初始化
_player = new Player(_recorder.recordName);
_player.addEventListener(PlayEvent.GET_DUREATON, onGetDurationHandle);
_player.addEventListener(PlayEvent.PLAY_END,onPlayEndHandle);
}else {
recordBtn.gotoAndStop("record");
_recorder.pauseRecord();
this.removeEventListener(Event.ENTER_FRAME, onReocrdingHandle);

//播放
playBtn.gotoAndStop("cando");
playBtn.addEventListener(MouseEvent.CLICK,onPlayBtnClickHandle);
}
}

private function onResetBtnClickHandle(event:MouseEvent) {
_recording = false
recordBtn.gotoAndStop("record");
resetBtn.gotoAndStop("notdo");
playBtn.gotoAndStop("notdo");
resetBtn.removeEventListener(MouseEvent.CLICK, onResetBtnClickHandle);
playBtn.removeEventListener(MouseEvent.CLICK,onPlayBtnClickHandle);
_recorder.stopRecord();
this.wave.bar.width = 0;
this.wave.line.graphics.clear();
_data.length = 0;
_recordingTime = 0;
this.removeEventListener(Event.ENTER_FRAME,onReocrdingHandle);
}

private function onRecorderProgressHandle(event:RecorderEvent) {
_recordingTime++;
this.wave.bar.width = _recordingTime / Recorder.FMS_TOTAL_TIME * 320;
trace("录制中"+_recordingTime);
}

private function onReocrdingHandle(event:Event) {
this.wave.line.graphics.clear();
this.wave.line.graphics.lineStyle(1, 0x00ff00, 1);
this.wave.line.graphics.moveTo(6, 0);
_data.shift();
_data.push(_mic.activityLevel);
for (var i:Number = 0; i < 308; i++) {
this.wave.line.graphics.lineTo(6+i, - _data[i]/6);
}
}

private function onRecorderEndHandle(event:RecorderEvent) {
// trace("垆埴完毕");
_recording = false;
recordBtn.gotoAndStop("record");
resetBtn.gotoAndStop("notdo");
playBtn.gotoAndStop("cando");
resetBtn.removeEventListener(MouseEvent.CLICK, onResetBtnClickHandle);
playBtn.addEventListener(MouseEvent.CLICK,onPlayBtnClickHandle);
_recorder.stopRecord();
_recordingTime = 0;
this.wave.bar.width = 0;
this.removeEventListener(Event.ENTER_FRAME,onReocrdingHandle);
}

private function onPlayBtnClickHandle(event:MouseEvent) {
_playing = !_playing;
if (_playing) {
//trace("播放");
recordBtn.gotoAndStop("notdo");
resetBtn.gotoAndStop("notdo");
playBtn.gotoAndStop("pause");
stopBtn.gotoAndStop("cando");
recordBtn.removeEventListener(MouseEvent.CLICK, onrecordBtnClickHandle);
resetBtn.removeEventListener(MouseEvent.CLICK,onResetBtnClickHandle);
resetBtn.removeEventListener(MouseEvent.CLICK, onResetBtnClickHandle);
stopBtn.addEventListener(MouseEvent.CLICK, onStopBtnClickHandle);
//播放
_player.play();
this.addEventListener(Event.ENTER_FRAME, onPlayingHandle);

this.wave.line.graphics.clear();
_data.length = 0;
//var musicWave:MusicWave = new MusicWave(320, 53);
//this.wave.addChild(musicWave);
}else {
//trace("暂停");
recordBtn.gotoAndStop("record");
resetBtn.gotoAndStop("notdo");
playBtn.gotoAndStop("cando");
stopBtn.gotoAndStop("notdo");
this.recordBtn.addEventListener(MouseEvent.CLICK, onrecordBtnClickHandle);
resetBtn.removeEventListener(MouseEvent.CLICK, onResetBtnClickHandle);
stopBtn.removeEventListener(MouseEvent.CLICK, onStopBtnClickHandle);

_recording = false;
_recorder.stopRecord();
_recordingTime = 0;
//暂停
_player.pause();
this.removeEventListener(Event.ENTER_FRAME,onPlayingHandle);
}

}

private function onGetDurationHandle(event:PlayEvent) {
_duration = _player.duration;
_stream = _player.stream;
this.wave.bar.width = 0;
this.addEventListener(Event.ENTER_FRAME,onPlayingHandle);
}

private function onPlayEndHandle(event:PlayEvent) {
this.removeEventListener(Event.ENTER_FRAME, onPlayingHandle);
onStopBtnClickHandle(null);
this.wave.bar.width = 0;
}

private function onPlayingHandle(event:Event) {
_nowTime = _stream.time;
if (this.wave.bar.width>=320) {
this.wave.bar.width = 0;
}else{
this.wave.bar.width = _nowTime / _duration * 320;
}
}
//播放停止
private function onStopBtnClickHandle(event:MouseEvent) {
//trace("播放停止");
recordBtn.gotoAndStop("record");
resetBtn.gotoAndStop("notdo");
playBtn.gotoAndStop("cando");
stopBtn.gotoAndStop("notdo");
this.recordBtn.addEventListener(MouseEvent.CLICK, onrecordBtnClickHandle);
resetBtn.removeEventListener(MouseEvent.CLICK, onResetBtnClickHandle);
stopBtn.removeEventListener(MouseEvent.CLICK, onStopBtnClickHandle);
_playing = false
_recording = false;
_recorder.stopRecord();
_recordingTime = 0;
//停止
_player.stop();
}

}

}




本文转自94cool博客园博客,原文链接:http://www.cnblogs.com/94cool/archive/2009/11/09/1599184.html,如需转载请自行联系原作者

相关文章
|
Kubernetes 监控 Dubbo
SpringCloud 应用在 Kubernetes 上的最佳实践 — 线上发布(优雅上下线)
本篇是《SpringCloud 应用在 Kubernetes 上的最佳实践》系列文章的第八篇,主要介绍了如何做到流量的无损上/下线。
5109 98
SpringCloud 应用在 Kubernetes 上的最佳实践 — 线上发布(优雅上下线)
|
XML JSON 算法
Neo4j-APOC扩展与使用(上)
Neo4j-APOC扩展与使用 1.APOC简介与安装 1.1 APOC简介 1.2安装APOC
Neo4j-APOC扩展与使用(上)
|
27天前
|
人工智能 自然语言处理 Shell
🦞 如何在 OpenClaw (Clawdbot/Moltbot) 配置阿里云百炼 API
本教程指导用户在开源AI助手Clawdbot中集成阿里云百炼API,涵盖安装Clawdbot、获取百炼API Key、配置环境变量与模型参数、验证调用等完整流程,支持Qwen3-max thinking (Qwen3-Max-2026-01-23)/Qwen - Plus等主流模型,助力本地化智能自动化。
36034 142
🦞 如何在 OpenClaw (Clawdbot/Moltbot) 配置阿里云百炼 API
|
9天前
|
人工智能 自然语言处理 监控
OpenClaw skills重构量化交易逻辑:部署+AI全自动炒股指南(2026终极版)
2026年,AI Agent领域最震撼的突破来自OpenClaw(原Clawdbot)——这个能自主规划、执行任务的智能体,用50美元启动资金创造了48小时滚雪球至2980美元的奇迹,收益率高达5860%。其核心逻辑堪称教科书级:每10分钟扫描Polymarket近千个预测市场,借助Claude API深度推理,交叉验证NOAA天气数据、体育伤病报告、加密货币链上情绪等多维度信息,捕捉8%以上的定价偏差,再通过凯利准则将单仓位严格控制在总资金6%以内,实现低风险高频套利。
4072 30
|
22天前
|
人工智能 安全 机器人
OpenClaw(原 Clawdbot)钉钉对接保姆级教程 手把手教你打造自己的 AI 助手
OpenClaw(原Clawdbot)是一款开源本地AI助手,支持钉钉、飞书等多平台接入。本教程手把手指导Linux下部署与钉钉机器人对接,涵盖环境配置、模型选择(如Qwen)、权限设置及调试,助你快速打造私有、安全、高权限的专属AI助理。(239字)
7968 22
OpenClaw(原 Clawdbot)钉钉对接保姆级教程 手把手教你打造自己的 AI 助手
|
21天前
|
人工智能 机器人 Linux
OpenClaw(Clawdbot、Moltbot)汉化版部署教程指南(零门槛)
OpenClaw作为2026年GitHub上增长最快的开源项目之一,一周内Stars从7800飙升至12万+,其核心优势在于打破传统聊天机器人的局限,能真正执行读写文件、运行脚本、浏览器自动化等实操任务。但原版全英文界面对中文用户存在上手门槛,汉化版通过覆盖命令行(CLI)与网页控制台(Dashboard)核心模块,解决了语言障碍,同时保持与官方版本的实时同步,确保新功能最快1小时内可用。本文将详细拆解汉化版OpenClaw的搭建流程,涵盖本地安装、Docker部署、服务器远程访问等场景,同时提供环境适配、问题排查与国内应用集成方案,助力中文用户高效搭建专属AI助手。
5393 12
|
5天前
|
存储 人工智能 负载均衡
阿里云OpenClaw多Agent实战宝典:从极速部署到AI团队搭建,一个人=一支高效军团
在AI自动化时代,单一Agent的“全能模式”早已无法满足复杂任务需求——记忆臃肿导致响应迟缓、上下文污染引发逻辑冲突、无关信息加载造成Token浪费,这些痛点让OpenClaw的潜力大打折扣。而多Agent架构的出现,彻底改变了这一现状:通过“单Gateway+多分身”模式,让一个Bot在不同场景下切换独立“大脑”,如同组建一支分工明确的AI团队,实现创意、写作、编码、数据分析等任务的高效协同。
938 21