我正在使用以下代码来获取设备的有效采样率,但对于My Samsung Galaxy Tab 1 OS版本2.3.6,它始终为audioRecorder.getState()
发送0,但对于Samsung Galaxy S2则很好。
对于Galaxy Tab,我总能得到desiredRate = -1;
public static void getValidSampleRates() {
int desiredRate = 0;
for (int rate : new int[] {44100, 8000, 11025, 16000, 22050}) { // add the rates you wish to check against
int bufferSize = AudioRecord.getMinBufferSize(rate, AudioFormat.CHANNEL_CONFIGURATION_DEFAULT, AudioFormat.ENCODING_PCM_16BIT);
if (bufferSize > 0) {
// buffer size is valid, Sample rate supported
AudioRecord audioRecorder = new AudioRecord(AudioSource.MIC, rate, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, 21168);
if (audioRecorder.getState() != AudioRecord.STATE_INITIALIZED)
desiredRate = -1;
else
desiredRate = rate;
}
}
}
我在 Android list 文件中添加了以下权限
<uses-sdk android:minSdkVersion="7" android:targetSdkVersion="14" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<application
请帮忙
最佳答案
我在这里看到两个问题:
bufferSizeInBytes
的硬编码值。根据documentation的说明,如果使用小于getMinBufferSize
的值,则会导致初始化失败。为什么不在构造函数中使用bufferSize
的值?另外,在调用channelConfig
时,请使用相同的getMinBufferSize
值。 new int[] {44100, 44100, 8000, 11025, 16000, 22050}
下面的代码块包括对代码的两项更改。请尝试一下,让我知道您得到了什么。
public static void getValidSampleRates() {
int desiredRate = 0;
for (int rate : new int[] {44100, 8000, 11025, 16000, 22050}) {
int bufferSize = AudioRecord.getMinBufferSize(rate, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT);
if (bufferSize > 0) {
// buffer size is valid, Sample rate supported
AudioRecord audioRecorder = new AudioRecord(AudioSource.MIC, rate, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, bufferSize);
if (audioRecorder.getState() != AudioRecord.STATE_INITIALIZED) {
desiredRate = -1;
audioRecorder.release();
} else {
desiredRate = rate;
break;
}
}
}
}