[英]Android audio recorder initialized failed
我嘗試使用如下所示的audiorecorder方法錄制聲音,在HTC nexus一個android 2.3上很好,但是當我在LG和Sony上使用andoid 4.0和4.1嘗試它時,它可以工作一次,下次嘗試時,它將在startRecording java.lang.IllegalStateException: startRecording() called on an uninitialized AudioRecord.
拋出異常java.lang.IllegalStateException: startRecording() called on an uninitialized AudioRecord.
怎么了?
final int RECORDER_BPP = 16;
int RECORDER_SAMPLERATE = 16000;
int RECORDER_CHANNELS = AudioFormat.CHANNEL_IN_MONO;
int RECORDER_AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT;
int bufferSizeInBytes = AudioRecord
.getMinBufferSize(RECORDER_SAMPLERATE, RECORDER_CHANNELS,
RECORDER_AUDIO_ENCODING);
// Initialize Audio Recorder.
AudioRecord audioRecorder = new AudioRecord(
MediaRecorder.AudioSource.MIC, RECORDER_SAMPLERATE,
RECORDER_CHANNELS, RECORDER_AUDIO_ENCODING, bufferSizeInBytes);
audioRecorder.startRecording();
不同的設備僅支持AudioRecord
某些設置。 有一項功能可以讓您檢測特定設備支持哪些設置。
private static int[] mSampleRates = new int[]{44100, 22050, 11025, 8000};
public AudioRecord findAudioRecord() {
for (int rate : mSampleRates) {
for (short audioFormat : new short[]{AudioFormat.ENCODING_PCM_8BIT, AudioFormat.ENCODING_PCM_16BIT}) {
for (short channelConfig : new short[]{AudioFormat.CHANNEL_IN_MONO, AudioFormat.CHANNEL_IN_STEREO}) {
try {
Log.d(TAG, "Attempting rate " + rate + "Hz, bits: " + audioFormat + ", channel: " + channelConfig);
int bufferSize = AudioRecord.getMinBufferSize(rate, channelConfig, audioFormat);
if (bufferSize != AudioRecord.ERROR_BAD_VALUE) {
// check if we can instantiate and have a success
AudioRecord recorder = new AudioRecord(MediaRecorder.AudioSource.DEFAULT, rate, channelConfig, audioFormat, bufferSize);
if (recorder.getState() == AudioRecord.STATE_INITIALIZED) {
return recorder;
}
}
} catch (Exception e) {
Log.e(TAG, rate + "Exception, keep trying.", e);
}
}
}
}
return null;
}
它會運行所有可用音頻格式設置的組合,並首先返回與您的設備能力匹配的內容。
您可以小幅修改它,甚至獲得設備支持的所有格式。
問題是由於未使用release(),因此在停止記錄器后應將其釋放,以便可以多次使用記錄器。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.