gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 00:21:37 29 4
gpt4 key购买 nike

我目前正在编写一个使用麦克风查询音量的应用程序。我正在使用 AlarmManager 每分钟查询一次声级。我面临的问题是,我发现如果我使用的另一个应用程序也使用麦克风(例如分贝级阅读器),我的应用程序将崩溃,因为麦克风不可用。有没有办法检查麦克风当前是否正在使用?

最佳答案

try catch 异常,因为当您尝试使用麦克风时遇到异常,您可以处理它。

“即使麦克风正在使用中,麦克风实际上也会准备好”

或这个代码 fragment 可能会给你一个想法

//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;
}

关于android - 如何检查android麦克风是否可用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22677749/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com