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]
二:
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/