简体   繁体   English

如何检查Android麦克风是否可用?

[英]How to check if android microphone is available for use?

I am currently programming an app that uses the microphone to query the sound level. 我目前正在编写一个使用麦克风查询声级的应用程序。 I am using AlarmManager to query the sound level every minute. 我正在使用AlarmManager每分钟查询一次声音水平。 The problem I am facing is that I discovered if I am using another app that uses the microphone too (eg decibel level reader) my app will crash because the microphone is not available. 我面临的问题是,我发现如果我也正在使用另一个也使用麦克风的应用程序(例如,分贝电平阅读器),则由于麦克风不可用,我的应用程序将崩溃。 Is there a way to check if the microphone is currently being used or not? 有没有办法检查当前是否正在使用麦克风?

Try Catching exception, as you get exception when you try to use microphone you can handle it. 尝试捕获异常,当您尝试使用麦克风时遇到异常时,可以处理它。

"The microphone will actually prepare fine even if the microphone is in use" “即使使用麦克风,麦克风实际上也会做好准备”

OR this code snippet may give you an idea 或此代码段可能会让您有个想法

//returns whether the microphone is available
    public static boolean getMicrophoneAvailable(Context context) {
        MediaRecorder recorder = new MediaRecorder();
            recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
        recorder.setOutputFile(new File(context.getCacheDir(), "MediaUtil#micAvailTestFile").getAbsolutePath());
        boolean available = true;
        try { 
            recorder.prepare();
            recorder.start();

        }
        catch (Exception exception) {
            available = false;
        }
        recorder.release();
        return available;
    }

if you use AudioRecord then call startRecording() and after that you should check recorder's state: getRecordingState() . 如果使用AudioRecord则调用startRecording() ,然后检查录音机的状态: getRecordingState() If recording was started successfully (it means mic is available), it will return 3 ( AudioRecord.RECORDSTATE_RECORDING ) otherwise it will return 1 ( AudioRecord.RECORDSTATE_STOPPED ) 如果录制成功开始(这意味着麦克风可用),它将返回3( AudioRecord.RECORDSTATE_RECORDING ),否则将返回1( AudioRecord.RECORDSTATE_STOPPED

Here's code for this function in Kotlin: 这是Kotlin中此功能的代码:

private fun isMicAvailable(audioRecord: AudioRecord): Boolean {
    audioRecord.startRecording()
    val isAvailable = audioRecord.recordingState == AudioRecord.RECORDSTATE_RECORDING
    audioRecord.stop()
    audioRecord.release()
    return isAvailable
}

I also want to detect whether microphone is being used or not. 我还想检测是否正在使用麦克风。 My solution is using AudioManager to get current mode. 我的解决方案是使用AudioManager来获取当前模式。

AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
if (am.getMode() == AudioManager.MODE_NORMAL){
   //microphone is available.
}

Other modes usage like MODE_IN_COMMUNICATION, MODE_IN_CALL, please check https://developer.android.com/reference/android/media/AudioManager.html#MODE_NORMAL 其他模式用法(如MODE_IN_COMMUNICATION,MODE_IN_CALL),请检查https://developer.android.com/reference/android/media/AudioManager.html#MODE_NORMAL

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM