如何检测Android上的语音是否可用?

Tom*_*Tom 3 java media android speech-recognition microphone

我相信我已经弄清楚如何检测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

        }
Run Code Online (Sandbox Code Playgroud)

但是,如何检测Android设备是否具有语音到文本功能?或者应该使用上述方法来检测?如果是这样,如何检测设备是否有麦克风?

任何反馈都表示赞赏,谢谢.

gui*_*ido 9

您附加的代码确实用于检测音频识别是否可用[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