当前位置: 代码迷 >> Android >> Android AudioRecord 施用注意事项
  详细解决方案

Android AudioRecord 施用注意事项

热度:87   发布时间:2016-05-01 17:15:29.0
Android AudioRecord 使用注意事项

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();	}
?
  相关解决方案