我相信我已经想出如何检测 android 设备是否有麦克风,如下所示:
Intent speechIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
List<ResolveInfo> speechActivities = packageManager.queryIntentActivities(speechIntent, 0);
TextView micAvailView = (TextView) findViewById(R.id.mic_available_flag);
if (speechActivities.size() != 0) { //we have a microphone
}
else { //we do not have a microphones
}
但是,如何检测 android 设备是否具有语音转文本功能?或者应该用上面的来检测?如果是这样,如何检测设备是否有麦克风?
欢迎任何反馈,谢谢。
最佳答案
您附上的代码确实是用来检测音频识别是否可用[1]:
// Check to see if a recognition activity is present PackageManager pm = getPackageManager(); List activities = pm.queryIntentActivities( new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0); if (activities.size() != 0) { speakButton.setOnClickListener(this); } else { speakButton.setEnabled(false); speakButton.setText("Recognizer not present"); }
要测试麦克风是否存在,只需遵循 [2] 中的代码和 [3] 中的文档,当调用 prepare() 时,如果麦克风不可用,您应该得到一个 IOException:
recorder = new MediaRecorder(); recorder.setAudioSource(MediaRecorder.AudioSource.MIC); recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); recorder.setOutputFile(path); recorder.prepare();
[1] http://developer.android.com/resources/articles/speech-input.html
[2] http://developer.android.com/guide/topics/media/index.html#capture
[3] http://developer.android.com/reference/android/media/MediaRecorder.AudioSource.html
关于java - 如何检测语音转文本是否在 Android 上可用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4770835/