80,350
社区成员
发帖
与我相关
我的任务
分享
public void testAudioRecordOP() throws Exception {
227 final int SLEEP_TIME = 10;
228 final int RECORD_TIME = 10000;
229 assertEquals(AudioRecord.STATE_INITIALIZED, mAudioRecord.getState());
230
231 int markerInFrames = mAudioRecord.getSampleRate() / 2;
232 assertEquals(AudioRecord.SUCCESS,
233 mAudioRecord.setNotificationMarkerPosition(markerInFrames));
234 assertEquals(markerInFrames, mAudioRecord.getNotificationMarkerPosition());
235 int periodInFrames = mAudioRecord.getSampleRate();
236 assertEquals(AudioRecord.SUCCESS,
237 mAudioRecord.setPositionNotificationPeriod(periodInFrames));
238 assertEquals(periodInFrames, mAudioRecord.getPositionNotificationPeriod());
239 OnRecordPositionUpdateListener listener = new OnRecordPositionUpdateListener() {
240
241 public void onMarkerReached(AudioRecord recorder) {
242 mIsOnMarkerReachedCalled = true;
243 }
244
245 public void onPeriodicNotification(AudioRecord recorder) {
246 mIsOnPeriodicNotificationCalled = true;
247 }
248 };
249 mAudioRecord.setRecordPositionUpdateListener(listener);
250
251 // use byte array as buffer
252 final int BUFFER_SIZE = 102400;
253 byte[] byteData = new byte[BUFFER_SIZE];
254 long time = System.currentTimeMillis();
255 mAudioRecord.startRecording();
256 assertEquals(AudioRecord.RECORDSTATE_RECORDING, mAudioRecord.getRecordingState());
257 while (System.currentTimeMillis() - time < RECORD_TIME) {
258 Thread.sleep(SLEEP_TIME);
259 mAudioRecord.read(byteData, 0, BUFFER_SIZE);
260 }
261 mAudioRecord.stop();
262 assertEquals(AudioRecord.RECORDSTATE_STOPPED, mAudioRecord.getRecordingState());
263 assertTrue(mIsOnMarkerReachedCalled);
264 assertTrue(mIsOnPeriodicNotificationCalled);
265 reset();
266
267 // use short array as buffer
268 short[] shortData = new short[BUFFER_SIZE];
269 time = System.currentTimeMillis();
270 mAudioRecord.startRecording();
271 assertEquals(AudioRecord.RECORDSTATE_RECORDING, mAudioRecord.getRecordingState());
272 while (System.currentTimeMillis() - time < RECORD_TIME) {
273 Thread.sleep(SLEEP_TIME);
274 mAudioRecord.read(shortData, 0, BUFFER_SIZE);
275 }
276 mAudioRecord.stop();
277 assertEquals(AudioRecord.RECORDSTATE_STOPPED, mAudioRecord.getRecordingState());
278 assertTrue(mIsOnMarkerReachedCalled);
279 assertTrue(mIsOnPeriodicNotificationCalled);
280 reset();
281
282 // use ByteBuffer as buffer
283 ByteBuffer byteBuffer = ByteBuffer.allocateDirect(BUFFER_SIZE);
284 time = System.currentTimeMillis();
285 mAudioRecord.startRecording();
286 assertEquals(AudioRecord.RECORDSTATE_RECORDING, mAudioRecord.getRecordingState());
287 while (System.currentTimeMillis() - time < RECORD_TIME) {
288 Thread.sleep(SLEEP_TIME);
289 mAudioRecord.read(byteBuffer, BUFFER_SIZE);
290 }
291 mAudioRecord.stop();
292 assertEquals(AudioRecord.RECORDSTATE_STOPPED, mAudioRecord.getRecordingState());
293 assertTrue(mIsOnMarkerReachedCalled);
294 assertTrue(mIsOnPeriodicNotificationCalled);
295 reset();
296
297 // use handler
298 final Handler handler = new Handler(Looper.getMainLooper()) {
299 @Override
300 public void handleMessage(Message msg) {
301 mIsHandleMessageCalled = true;
302 super.handleMessage(msg);
303 }
304 };
305
306 mAudioRecord.setRecordPositionUpdateListener(listener, handler);
307 time = System.currentTimeMillis();
308 mAudioRecord.startRecording();
309 assertEquals(AudioRecord.RECORDSTATE_RECORDING, mAudioRecord.getRecordingState());
310 while (System.currentTimeMillis() - time < RECORD_TIME) {
311 Thread.sleep(SLEEP_TIME);
312 mAudioRecord.read(byteData, 0, BUFFER_SIZE);
313 }
314 mAudioRecord.stop();
315 assertEquals(AudioRecord.RECORDSTATE_STOPPED, mAudioRecord.getRecordingState());
316 assertTrue(mIsOnMarkerReachedCalled);
317 assertTrue(mIsOnPeriodicNotificationCalled);
318 // The handler argument is only ever used for getting the associated Looper
319 assertFalse(mIsHandleMessageCalled);
320
321 mAudioRecord.release();
322 assertEquals(AudioRecord.STATE_UNINITIALIZED, mAudioRecord.getState());
323 }