Android(安卓)AudioRecord 使用注意事项
16lz
2021-01-24
AudioRecord对象需要在Activity的线程里面创建。读取数据时可以在独立的线程里面进行。否则华为U8800之类手机录音时会出错。
public VoiceRecorder(Tranceiver tx, int sampleRate)throws IllegalArgumentException, IllegalStateException {this.tx = tx;final int bufSize = AudioRecord.getMinBufferSize(sampleRate,CHANNEL_CONFIGURATION_MONO, ENCODING_PCM_16BIT);this.rec = new AudioRecord(MediaRecorder.AudioSource.MIC, sampleRate,CHANNEL_CONFIGURATION_MONO, ENCODING_PCM_16BIT, bufSize);rec.startRecording();}public void run() {final byte[] buf = new byte[8 * 1024];while (running) {int n = rec.read(buf, 0, buf.length);if (n < 1)continue;try {tx.send(buf, 0, n);} catch (InterruptedException e) {Log.w(TAG, e);}}try {rec.stop();} catch (IllegalStateException e) {Log.w(TAG, e);}rec.release();}
更多相关文章
- 在线升级Android应用程序完善版
- Android(安卓)handler src track
- Android(安卓)-HandleThread-给线程传参数
- Android(安卓)APK+Dex文件反编译及回编译工具
- Android(安卓)更新ProgressBar
- android线程间通信和主线程更新ui
- Android(安卓)6.0打开串口返回-1问题
- Android下的JNI创建多线程的方法
- 理解杀进程的实现原理