美文网首页
Android判断麦克风是否被占用

Android判断麦克风是否被占用

作者: monkey_who | 来源:发表于2018-12-06 14:55 被阅读0次

    返回true就是没有被占用。
    返回false就是被占用。
    代码来源:
    https://stackoverflow.com/questions/35633513/how-to-check-whether-microphone-is-used-by-any-background-app

    private boolean validateMicAvailability(){
        Boolean available = true;
        AudioRecord recorder =
                new AudioRecord(MediaRecorder.AudioSource.MIC, 44100,
                        AudioFormat.CHANNEL_IN_MONO,
                        AudioFormat.ENCODING_DEFAULT, 44100);
        try{
            if(recorder.getRecordingState() != AudioRecord.RECORDSTATE_STOPPED ){
                available = false;
    
            }
    
            recorder.startRecording();
            if(recorder.getRecordingState() != AudioRecord.RECORDSTATE_RECORDING){
                recorder.stop();
                available = false;
    
            }
            recorder.stop();
        } finally{
            recorder.release();
            recorder = null;
        }
    
        return available;
    }
    

    相关文章

      网友评论

          本文标题:Android判断麦克风是否被占用

          本文链接:https://www.haomeiwen.com/subject/wolocqtx.html