如何查询默认语音识别器

2022-09-03 15:20:31

如何找出默认系统语音识别器的组件名称,即调用 createSpeechRecognizer(上下文上下文)时返回的组件名称?(实际上,我只需要找出它支持哪些输入语言,所以如果只有答案,那么我也会很感激。

该框架通过以下方式解决了这个问题。

String serviceComponent = Settings.Secure.getString(mContext.getContentResolver(),
                        Settings.Secure.VOICE_RECOGNITION_SERVICE);

(请参阅 SpeechRecognizer 的源代码

但是,此解决方案似乎不适用于第三方应用程序。


答案 1

但是,此解决方案似乎不适用于第三方应用程序。

我假设你得出这样的结论,因为它不是一个公共API。但是,Settings.Secure.getString() 要求行名才能在安全表中查找第二个参数。因此,您只需提供要查找的行的实际名称:“voice_recognition_service”。Settings.Secure.VOICE_RECOGNITION_SERVICE

也就是说,您可以使用相同的代码,只需稍作更改:SpeechRecognizer

String serviceComponent = Settings.Secure.getString(mContext.getContentResolver(),
        "voice_recognition_service");

希望这有帮助。


答案 2

更新(我误读了原始问题)

SpeechRecognizer 不是执行语音处理的对象,但是,传递给 SpeechRecognizer 的 Intent 是 (via )。该意图使用和AFAIK,可以以老式的方式检测。startListening(Intent intent)RecognizerIntent.ACTION_RECOGNIZE_SPEECH

要检测默认值,请尝试解析您希望查找默认值但具有集合的 Intent。PackageManager.MATCH_DEFAULT_ONLY

未经测试的代码:

String detectDefaultSpeechRecognizer(Context context) {
  final Intent speechIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
  // 1: Try to find the default speech intent
  final ResolveInfo defaultResolution = context.getPackageManager().resolveService(speechIntent, PackageManager.MATCH_DEFAULT_ONLY);
  if (defaultResolution != null) {
    final ActivityInfo activity = defaultResolution.activityInfo;
    if (!activity.name.equals("com.android.internal.app.ResolverActivity")) {
      //ResolverActivity was launched so there is no default speech recognizer
      return "";
    }
  }
  // 2: Try to find anything that we can launch speech recognition with. Pick up the first one that can.
  final List<ResolveInfo> resolveInfoList = context.getPackageManager().queryIntentServices(speechIntent, PackageManager.MATCH_DEFAULT_ONLY);
  if (!resolveInfoList.isEmpty()) {
    speechIntent.setClassName(resolveInfoList.get(0).activityInfo.packageName, resolveInfoList.get(0).activityInfo.name);
    return resolveInfoList.get(0).activityInfo.packageName;
  }
  return "";
}

旧答案

看看GAST,它有一种方法可以检查语音识别器中是否支持某种语言。
https://github.com/gast-lib/gast-lib/blob/master/library/src/root/gast/speech/SpeechRecognizingActivity.java#L70

您也可以尝试手动检查元数据标记。http://developer.android.com/reference/android/speech/RecognitionService.html#SERVICE_META_DATA<recognition-service>


推荐