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(); }?