【发布时间】:2015-11-26 10:48:25
【问题描述】:
我有一个录音机应用程序代码,基于麦克风状态。我想显示状态,例如,如果麦克风已被其他应用程序使用,则显示状态如麦克风已在使用,否则正在录制......就像那样,所以我使用下面的代码在录音机中获取麦克风状态。 getRecordingState() 但它总是返回 1,即使我正在通话或空闲。帮我一些代码来获取麦克风状态。提前致谢。
int[] mSampleRates = new int[]{8000, 11025, 22050, 44100};
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) {
Log.i("", "Recorder intialised " + recorder.getRecordingState() + " " + recorder.getState() + " ");
return recorder;
}
}
} catch (Exception e) {
Log.e(TAG, rate + "Exception, keep trying.", e);
}
}
}
}
return null;
【问题讨论】:
-
谢谢sunny,我不知道HAL层,你能帮我从应用程序级别获取状态
标签: android android-audiomanager android-audiorecord