如何检查的Andr​​oid麦克风可供使用? [英] How to check if android microphone is available for use?

查看:201
本文介绍了如何检查的Andr​​oid麦克风可供使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前编程使用麦克风来查询声级的应用程序。我使用AlarmManager查询声级的每一分钟。我现在面临的问题是,我发现,如果我使用一个使用麦克风过另一个应用程序(例如分贝读者)我的应用程序会崩溃,因为麦克风是不可用的。有没有一种方法来检查,如果当前正在使用或没有麦克风?

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. The problem I am facing is that I discovered if I am using another app that uses the microphone too (e.g. 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.

必竟是prepare罚款,即使麦克风在使用麦克风

"The microphone will actually prepare fine even if the microphone is in use"


这code片段可以给你一个想法

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();
        }
        catch (IOException exception) {
            available = false;
        }
        recorder.release();
        return available;
    }

这篇关于如何检查的Andr​​oid麦克风可供使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆