我正在做一个演讲重组项目。我有一个util类,它由三种方法组成:检查麦克风是否存在、检查麦克风的可用性和检查文本到语音的可用性。有人能检查我的密码吗?
public class MediaUtil {
//returns whether a microphone exists
public boolean getMicrophoneExists(Context context) {
PackageManager packageManager = context.getPackageManager();
return packageManager.hasSystemFeature(PackageManager.FEATURE_MICROPHONE);
}
//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;
}
//returns whether text to speech is available
public static boolean getTTSAvailable(Context context) {
PackageManager packageManager = context.getPackageManager();
Intent speechIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
List<ResolveInfo> speechActivities = packageManager.queryIntentActivities(speechIntent, 0);
if (speechActivities.size() != 0) return true;
return false;
}发布于 2015-12-02 22:35:29
getMicrophoneAvailable时,它可能是可用的,但后来当你的应用程序真正需要它时,它已经被其他应用程序成功地声称了。这意味着没有必要对可用性进行测试。根据需要获取它:公共静态MediaRecorder getMicrophone(上下文上下文){.尝试{ recorder.prepare();} catch (IOException异常){返回null;}返回记录器;}https://codereview.stackexchange.com/questions/112668
复制相似问题