我已经阅读了很多关于 AudioRecorder 初始化问题的帖子,所以我使用了一个函数来测试每一种可能性。不幸的是,没有一个有效的。我应该检查什么?
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.w("DEBUG", "Audio record");
// Capture mono data at 16kHz
int frequency = 44100;
int channelConfiguration = AudioFormat.CHANNEL_IN_MONO;
int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
AudioRecord audioRecord=null;
// The minimal buffer size CANNOT be merely 20ms of data, it must be
// at least 1024 bytes in this case, this is most likely due to a MMIO
// hardware limit.
final int bufferSize = AudioRecord.getMinBufferSize(frequency, channelConfiguration, audioEncoding);
Log.w("DEBUG", "Buffer size: "+bufferSize);
try
{
// audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,
// frequency, channelConfiguration,
// audioEncoding, bufferSize);
audioRecord=findAudioRecord();
short[] buffer = new short[bufferSize];
if(audioRecord==null || audioRecord.getState()!= AudioRecord.STATE_INITIALIZED)
{
Log.w("DEBUG", "Can't start");
//Log.w("DEBUG", "status: " + audioRecord.getState());
return;
}
audioRecord.startRecording();
// Blocking loop uses about 40% of the CPU to do this.
int sampleNumber = 0;
// We'll capture 3000 samples of 20ms each,
// giving us 60 seconds of audio data.
while (sampleNumber < 3000)
{
audioRecord.read(buffer, 0, 320);
// for (int i = 0; i < buffer.length; i++)
// {
// fileBuffer[i * 2] = (byte) (buffer[i] & (short) 0xFF);
// fileBuffer[i * 2 + 1] = (byte) (buffer[i] >> 8);
// }
sampleNumber++;
}
} catch (IllegalArgumentException e)
{
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (IllegalStateException e)
{
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
finally
{
if(audioRecord!=null && audioRecord.getState()==AudioRecord.STATE_INITIALIZED)
{
audioRecord.stop();
audioRecord.release();
}
}
}
private static int[] mSampleRates = new int[] { 8000, 11025, 22050, 44100 };
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.w("DEBUG", "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.w("DEBUG", rate + "Exception, keep trying.",e);
}
}
}
}
return null;
}
这是输出:
10-06 22:18:33.828: WARN/DEBUG(26097): Attempting rate 8000Hz, bits: 3, channel: 16 10-06 22:18:33.828: WARN/DEBUG(26097): Attempting rate 8000Hz, bits: 3 ,通道:12 10-06 22:18:33.828:WARN/DEBUG(26097):尝试率 8000Hz,位:2,通道:16 10-06 22:18:33.867:WARN/DEBUG(26097):尝试率 8000Hz , 位: 2, 通道: 12 10-06 22:18:33.875: WARN/DEBUG(26097): 尝试率 11025Hz, 位: 3, 通道: 16 10-06 22:18:33.875: WARN/DEBUG(26097) :尝试速率 11025Hz,位:3,通道:12 10-06 22:18:33.875:WARN/DEBUG(26097):尝试速率 11025Hz,位:2,通道:16 10-06 22:18:33.906:WARN/调试(26097):尝试速率 11025Hz,位:2,通道:12 10-06 22:18:33.929:警告/调试(26097):尝试速率 22050Hz,位:3,通道:16 10-06 22:18: 33.929:警告/调试(26097):尝试速率 22050Hz,位:3,通道:12 10-06 22:18:33.929:WARN/DEBUG(26097):尝试率 22050Hz,位:2,通道:16 10-06 22:18:33.929:WARN/DEBUG(26097):尝试率 22050Hz,位:2,通道:12 10-06 22:18:33.929:WARN/DEBUG(26097):尝试率 44100Hz,位:3,通道:16 10-06 22:18:33.929:WARN/DEBUG(26097):尝试速率 44100Hz,位:3,通道:12 10-06 22:18:33.929:WARN/DEBUG(26097):尝试速率 44100Hz,位:2,通道:16 10-06 22:18:33.937:WARN/DEBUG (26097):尝试速率 44100Hz,位:2,通道:12 10-06 22:18:33.937:警告/调试(26097):无法启动通道:16 10-06 22:18:33.929:WARN/DEBUG(26097):尝试速率 44100Hz,位:3,通道:12 10-06 22:18:33.929:WARN/DEBUG(26097):尝试速率 44100Hz,位:2,通道:16 10-06 22:18:33.937:警告/调试(26097):尝试率 44100Hz,位:2,通道:12 10-06 22:18:33.937:警告/调试(26097):无法启动通道:16 10-06 22:18:33.929:WARN/DEBUG(26097):尝试速率 44100Hz,位:3,通道:12 10-06 22:18:33.929:WARN/DEBUG(26097):尝试速率 44100Hz,位:2,通道:16 10-06 22:18:33.937:警告/调试(26097):尝试率 44100Hz,位:2,通道:12 10-06 22:18:33.937:警告/调试(26097):无法启动
我在清单中有以下内容:
使用-sdk android:minSdkVersion="10" android:targetSdkVersion="16" 使用-permission android:name="android.permission.RECORD_AUDIO"
我的手机是 Galaxy Ace。我应该检查什么?