AudioRecord 使用小结与注意事项

使用专门的线程读取数据,容易出现buffer overflow。
使用 setRecordPositionUpdateListener时,要注意两个问题:

一:
1.启动录音后,要先read buffer,才会通知激活 listener。

audioRecord.startRecording(); audioRecord.read(buffer, 0, buffer.length);


2.录音 通知周期 及 录音数据读取 buffer 的设定

audioRecord.setPositionNotificationPeriod(framePeriod);

//也可以自己指定一个buffer大小,但是不能小于min buffer

bufferSize = AudioRecord.getMinBufferSize(sampleRate,channelConfig, audioFormat);

int framePeriod=framePeriod = bufferSize / (2 * bSamples * nChannels / 8);

//读取录音数据 的buffer 应该为bufferSize/2,否则会出现stop后,数据延时与混乱(数据未正确写入文件)

buffer = new byte[framePeriod * bSamples / 8 * nChannels];//buffer = new byte[bufferSize/2]


二:
避免录音阻塞主线程,利用反射,更改AudioRecord 对象中的  mInitializationLooper(Looper 对象) 属性,为我们指定的子线程的 looper,然后再设置 setRecordPositionUpdateListener(这个方法中会用到 mInitializationLooper 对象

audioRecorder1=AudioRecorder1.getInstanse();

HandlerThread handlerThread=new HandlerThread("handlerThread");
handlerThread.start();

...

//利用反射,更改属性,为指定子线程的looper

field2.set(field.get(audioRecorder1), handlerThread.getLooper());

//设置监听

audioRecorder1.prepare();

//prepare 方法的实现

public void prepare() { try { if (state == State.INITIALIZING) { audioRecorder.setRecordPositionUpdateListener(updateListener); audioRecorder.setPositionNotificationPeriod(framePeriod); if ((audioRecorder.getState() == AudioRecord.STATE_INITIALIZED) & (filePath != null)) { // write file header OutputStream out = new FileOutputStream(filePath); out.write(1); out.close(); random = new RandomAccessFile(filePath, "rw"); random.setLength(0); // Set file length to random.writeBytes("RIFF");

......


具体细节如下

audioRecorder1=AudioRecorder1.getInstanse(); HandlerThread handlerThread=new HandlerThread("handlerThread"); handlerThread.start(); try { Field field=audioRecorder1.getClass().getDeclaredField("audioRecorder"); Field field2=field.getType().getDeclaredField("mInitializationLooper"); field.setAccessible(true); field2.setAccessible(true); System.out.println("--handlerThread-lp--"+handlerThread.getLooper()); System.out.println("--main-lp--"+Looper.getMainLooper()); field2.set(field.get(audioRecorder1), handlerThread.getLooper()); audioRecorder1.setOutputFile("/mnt/sdcard/audio11.wav"); audioRecorder1.prepare(); audioRecorder1.start(); } catch (NoSuchFieldException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); }


转自:http://luohaoxuan320.blog.163.com/blog/static/207784109201210741957664/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值