Sound Recorder With Adobe Air

Run baby run
图和文没有任何关系
由于我需要在我的mini项目中录制音频,所以我去找了一下Air录制音频的例子,发现很早以前就已经有人在讨论这个问题了,地址如下。

Saving Microphone Recording IOS/Android我担心的是,这些问题是大约2年前被讨论的,里面涉及到的工具包和代码库是否仍然能在iOS7下面运行呢,答案是,YES!他们一样能正常工作,我的实现方案分成如下步骤,录音,保存成WAV,然后回放。其中录音和保存输出的部分,在这个开源项目中都已经达成了,micrecorder,我又找到了一个忘记什么时候找到的兄台封装的代码,如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package utils  
{
import flash.events.Event;
import flash.events.SampleDataEvent;
import flash.filesystem.File;
import flash.filesystem.FileMode;
import flash.filesystem.FileStream;
import flash.media.Microphone;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.utils.ByteArray;
import org.bytearray.micrecorder.encoder.WaveEncoder;
public class SoundRecorder
{
public function SoundRecorder()
{
init();
}
private var enco:WaveEncoder=new WaveEncoder();
private var file:File;
private var fs:FileStream = new FileStream();
private var ch:SoundChannel;
//ByteArray in which the recorded sound data is stored
private var soundBytes:ByteArray = new ByteArray();
//ByteArray from which the recorded sound data is played
private var soundO:ByteArray = new ByteArray();
//Sound object which plays the recorded sound...
private var sound:Sound= new Sound();
//Gets your default microphone
private var mic:Microphone;
//To check whether the application is recording the sound or not
private var recMode:Boolean=false;
//To check whether the application is playing the sound or not
private var playMode:Boolean=false;
//function called at start of application
private function init():void{
mic = Microphone.getMicrophone();
//Sets the minimum input level that should be considered sound
mic.setSilenceLevel(0);
//The amount by which the microphone boosts the signal.
mic.gain = 100;
//The rate at which the microphone is capturing sound, in kHz.
mic.rate = 44;
}
//function called when start Record button is clicked
private function startRecord():void
{
mic.addEventListener(SampleDataEvent.SAMPLE_DATA, micSampleDataHandler);
}
//function called when stop Record button is clicked
private function stopRecord():void
{
mic.removeEventListener(SampleDataEvent.SAMPLE_DATA, micSampleDataHandler);
soundBytes.position = 0;
soundO.length=0
soundO.writeBytes(soundBytes);
soundO.position = 0;
soundBytes.length=0;
}
private function micSampleDataHandler(event:SampleDataEvent):void
{
while (event.data.bytesAvailable)
{
var sample:Number = event.data.readFloat();
soundBytes.writeFloat(sample);
}
}
private function playSound():void
{
soundO.position = 0;
sound.addEventListener(SampleDataEvent.SAMPLE_DATA, playbackSampleHandler);
ch=sound.play();
ch.addEventListener(Event.SOUND_COMPLETE,onSC)
}
private function stopSound():void
{
sound.removeEventListener(SampleDataEvent.SAMPLE_DATA, playbackSampleHandler);
ch.stop()
ch.removeEventListener(Event.SOUND_COMPLETE,onSC)
}
private function onSC(evt:Event):void
{
stopSound()
soundO.position=0
playMode=!true
}
private function playbackSampleHandler(event:SampleDataEvent):void
{
for (var i:int = 0; i < 8192; i++)
{
if (soundO.bytesAvailable < 4)
{
break
}
var sample:Number = soundO.readFloat();
event.data.writeFloat(sample);
event.data.writeFloat(sample);
}
}
public function onSave(fileName:String):void
{
soundO.position = 0
var o:ByteArray = enco.encode(soundO,2,16);
file = File.applicationStorageDirectory.resolvePath("wav/"+fileName);
fs.open(file,FileMode.WRITE);
fs.writeBytes(o);
fs.close();

}
public function onRecord():void
{
if(!recMode){
recMode=true
startRecord()
}else{
recMode = !true;
stopRecord();
}
}
public function onPlay():void
{
if(!playMode){
playMode=true
playSound()
}else{
playMode=!true;
stopSound();
}
}
}
}

这里需要注意的是,不要在程序启动瞬间马上初始化它,
否则 mic = Microphone.getMicrophone();会失效,录制不到任何声音。

之后遇到的问题就是回放WAV格式的文件,利用了这里的库,as3wavsound,回放的代码如下:

1
2
3
4
5
6
7
8
var bytes:ByteArray = new ByteArray();  
var inputFile:File = File.applicationStorageDirectory.resolvePath("wav/b.wav") ;
var inputStream:FileStream = new FileStream();
inputStream.open(inputFile,FileMode.READ);
inputStream.readBytes(bytes,0,inputStream.bytesAvailable);
inputStream.close();
var wavPlayer:WavSound = new WavSound(bytes);
wavPlayer.play(0,0);