This repository has been archived by the owner on Feb 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Main.as
250 lines (200 loc) · 6.34 KB
/
Main.as
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package
{
import flash.text.TextField;
import flash.text.TextFormat;
import fl.transitions.Tween;
import fl.transitions.easing.Strong;
import flash.display.Sprite;
import flash.utils.setTimeout;
import flash.display.MovieClip;
import flash.media.Microphone;
import flash.system.Security;
import org.bytearray.micrecorder.*;
import org.bytearray.micrecorder.events.RecordingEvent;
import org.bytearray.micrecorder.encoder.WaveEncoder;
import org.as3wavsound.WavSound;
import flash.events.MouseEvent;
import flash.events.SampleDataEvent;
import flash.events.Event;
import flash.events.ActivityEvent;
import flash.net.FileReference;
public class Main extends Sprite
{
private var mic:Microphone;
private var waveEncoder:WaveEncoder = new WaveEncoder();
private var recorder:MicRecorder = new MicRecorder(waveEncoder);
private var recBar:RecBar = new RecBar();
private var tween:Tween;
private var fileReference:FileReference = new FileReference();
private var recorded:WavSound;
private var date = new Date();
private var recorded_once:Boolean = false;
private var recording:Boolean = false;
private var playing:Boolean = false;
private var rec_title:TextField = new TextField();
private var title_format:TextFormat;
private const MIN_LENGTH:uint = 1000;
private var recButton = new btn_r();
private var playButton = new btn_p();
private var saveButton = new btn_d();
private var vu_meter = new vumeter();
public function Main():void
{
title_format = new TextFormat();
title_format.align = 'center';
title_format.size = 20;
title_format.font = 'HelveticaNeue LT 65 Medium';
title_format.color = 0xAC1300;
rec_title.defaultTextFormat = title_format;
if(loaderInfo.parameters.title)
{
rec_title.text = loaderInfo.parameters.title;
}
else
{
rec_title.text = 'Simple AS3 Recorder';
}
rec_title.width = stage.width;
rec_title.height = 25;
rec_title.y = 35;
rec_title.selectable = false;
recButton.y = playButton.y = saveButton.y = 105;
recButton.x = 69;
playButton.x = vu_meter.x = 160;
saveButton.x = 251;
vu_meter.y = 150;
vu_meter.alpha = 1;
vu_meter.height = 10;
addChild(rec_title);
addChild(recButton);
addChild(playButton);
addChild(saveButton);
addChild(vu_meter);
updateButtons( [1, 3] );
saveButton.gotoAndStop(3);
vu_meter.stop();
mic = Microphone.getMicrophone();
mic.setSilenceLevel(0);
mic.gain = 100;
mic.setLoopBack(false);
mic.setUseEchoSuppression(true);
Security.showSettings('2');
addListeners();
}
public function addListeners():void
{
recButton.addEventListener(MouseEvent.MOUSE_UP, startRecording);
playButton.addEventListener(MouseEvent.MOUSE_UP, startPlaying);
saveButton.addEventListener(MouseEvent.MOUSE_UP, saveRecording);
recorder.addEventListener(RecordingEvent.RECORDING, disp_recording);
recorder.addEventListener(Event.COMPLETE, recordComplete);
vu_meter.addEventListener(Event.ENTER_FRAME, updateMeter);
}
/* --- PROCESSING FUNCTIONS --- */
public function startRecording(e:MouseEvent):void
{
if (mic != null && !playing)
{
recording = true;
recorder.record();
e.target.gotoAndStop(2);
if(saveButton.curentFrame != 3)
saveButton.gotoAndStop(3);
if(playButton.curentFrame != 3)
playButton.gotoAndStop(3);
recButton.removeEventListener(MouseEvent.MOUSE_UP, startRecording);
recButton.addEventListener(MouseEvent.MOUSE_UP, stopRecording);
recBar.width = stage.width;
recBar.height = 40;
addChild(recBar);
tween = new Tween(recBar,"y",Strong.easeOut, - (recBar.height/2+10),0,1,true);
}
}
public function stopRecording(e:MouseEvent):void
{
recorder.stop();
mic.setLoopBack(false);
e.target.gotoAndStop(1);
recButton.removeEventListener(MouseEvent.MOUSE_UP, stopRecording);
recButton.addEventListener(MouseEvent.MOUSE_UP, startRecording);
tween = new Tween(recBar,"y",Strong.easeOut,0, - (recBar.height/2+10),1,true);
}
public function recordComplete(e:Event):void
{
recording = false;
recorded = new WavSound( recorder.output );
if(recorded.length > MIN_LENGTH)
{
recorded_once = true;
playButton.gotoAndStop(1);
saveButton.gotoAndStop(1);
}
}
public function startPlaying(e:Event):void
{
if(recorded_once && !playing && !recording)
{
playing = true;
recorded.play();
setTimeout(updateButtons, 1000, [3, 2] );
//updateButtons( [3, 2] );
playButton.removeEventListener(MouseEvent.MOUSE_UP, startPlaying);
playButton.addEventListener(MouseEvent.MOUSE_UP, stopPlaying);
setTimeout(stopPlaying, recorded.length, new MouseEvent(MouseEvent.MOUSE_UP));
}
}
public function stopPlaying(e:Event):void
{
playing = false;
recorded.stop();
setTimeout(updateButtons, 1000, [1, 1] );
playButton.removeEventListener(MouseEvent.MOUSE_UP, stopPlaying);
playButton.addEventListener(MouseEvent.MOUSE_UP, startPlaying);
}
public function saveRecording(e:Event):void
{
if(!recording && recorded_once && recorded.length > MIN_LENGTH)
{
var file_name:String;
if(date.getMonth() < 10)
file_name = '0' + date.getMonth() + '-';
else
file_name = date.getMonth() + '-';
if(date.getDate() < 10)
file_name += '0' + date.getDate() + '-' + date.getFullYear();
else
file_name += date.getDate() + '-' + date.getFullYear();
file_name = 'e-Client ' +
file_name
+ '_' +
date.getHours()
+'.'+
date.getMinutes();
fileReference.save(recorder.output, file_name + '.wav');
}
}
/* --- DISPLAY FUNCTIONS --- */
private function disp_recording(e:RecordingEvent):void
{
var currentTime:int = Math.floor(e.time / 1000);
recBar.counter.text = String(currentTime);
if (String(currentTime).length == 1)
{
recBar.counter.text = "00:0" + currentTime;
}
else if (String(currentTime).length == 2)
{
recBar.counter.text = "00:" + currentTime;
}
}
private function updateMeter(e:Event):void
{
vu_meter.gotoAndPlay(100 - mic.activityLevel);
}
private function updateButtons(frames:Array):void
{
recButton.gotoAndStop(frames[0]);
playButton.gotoAndStop(frames[1]);
}
} //fin de classe
} //fin de package