且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何检测语音到文本是否在Android上可用?

更新时间:2023-12-04 21:14:52

您随附的代码确实用于检测音频识别是否可用[1]:

The code you attached is indeed used to detect if audio recognition is available [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:

To test if a mic is present, just follow the code in [2] and the docs at [3], when calling prepare() you should get an IOException if the mic is not available:


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

[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