【问题标题】:SpeechRecognizer on Android device without Google Apps没有 Google Apps 的 Android 设备上的 SpeechRecognizer
【发布时间】:2016-06-16 10:47:40
【问题描述】:

SpeechRecognizer 在带有 Google Apps (GApps) 的 Android 上运行良好。但是在中国,大多数 Android 设备都会删除这些 Google Apps。使用SpeechRecognizer 会发生什么?没有实际设备我怎么能测试它?

speechRecognizer = SpeechRecognizer.createSpeechRecognizer(getApplicationContext());
speechRecognizer.setRecognitionListener(new CustomListener());
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "zh_HK");
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getClass().getPackage().getName());
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
intent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, false);
intent.putExtra("android.speech.extra.EXTRA_ADDITIONAL_LANGUAGES", new String[]{"zh_HK", "en-US"});
speechRecognizer.startListening(intent);

CustomListener() 在哪里实现RecognitionListener

【问题讨论】:

  • 一种可能性是通过 PackageManager 检查是否安装了 Gapps,例如检查 com.android.vending。如果没有,那么语音识别的另一种方法可能是 sphinx:cmusphinx.sourceforge.net
  • 好主意。但是,Sphinx 的语言文件占用了很多空间。如果不添加检查,上面的代码会崩溃吗?
  • 嗯,这是一个很好的问题,如果可以的话,我会对此给予更多的支持。但真的......我不知道。如果我从正常情况开始,如果没有包来执行此操作,则处理某些操作的 Intent 将崩溃。我认为缺少gapps也是一样,为什么要以另一种方式处理。避免这种情况可以通过 intent.resolveActivity(pckManager) != null` 来完成,我猜。
  • 如果您有一些未使用的设备并安装 cyanogenmod 没有间隙,也许您可​​以测试它。
  • 另一个想法是使用 com.android.vending<uses-feature> 设置为清单,但我从未尝试为软件提供使用功能。但它在 API 中有描述,for hardware or software......

标签: android speech-recognition


【解决方案1】:

我相信您知道以下大部分内容,但为了获得全面的答案:

只要正确注册RecognitionService,任何应用程序都可以注册为语音识别提供程序。对于三星设备,Android 语音搜索设置将显示两个提供商,谷歌和 Vlingo。

Google 的 RecognitionService 封装在他们的 Google 'Now' 应用程序中,正如您所知,它依赖于 Google Play 服务。

Vlingo 的 RecognitionService 包含在他们的 S-Voice 应用程序中,该应用程序只能公开预装在三星设备上 - 因此并不真正适用于您的问题,但由于我的评论进一步下方,我提到了。

在使用SpeechRecognizer 之前,您应该始终使用静态辅助方法:

if (SpeechRecognizer.isRecognitionAvailable(getApplicationContext())) {
    // initialise
   } else {
   // nope
   }

正如method documentation所引用的:

检查语音识别服务是否可用 系统。如果此方法返回 false,则 createSpeechRecognizer(Context) 会失败。

如果识别可用则返回真,否则返回假

如果您在特定用例中使用此方法,它应该返回 false,因此您不必担心初始化崩溃。

请注意,Vlingo 将在此处返回 true,但不会真正返回语音响应,它只会抛出 ERROR_NETWORK 出于某种原因。很烦人。

除了上述检查之外,您还可以通过执行以下操作来查询哪些应用程序(如果有)注册为语音识别提供程序:

final List<ResolveInfo> services = context.getPackageManager().queryIntentServices(
                    new Intent(RecognitionService.SERVICE_INTERFACE), 0);

任何空列表都意味着没有可用的提供程序。

最后,如 cmets 中所述,您可以随时检查并确保已安装 Google 应用程序:

假设您的目标是 Jelly Bean+,我使用以下便捷方法:

/**
 * Check if the user has a package installed
 *
 * @param ctx         the application context
 * @param packageName the application package name
 * @return true if the package is installed
 */
public static boolean isPackageInstalled(@NonNull final Context ctx, @NonNull final String packageName) {
    try {
        ctx.getApplicationContext().getPackageManager().getApplicationInfo(packageName, 0);
        return true;
    } catch (final PackageManager.NameNotFoundException e) {
        return false;
    }
}

其中 Google 的包名是 com.google.android.googlequicksearchbox

最后,我相信您知道还有许多其他语音识别提供商可以提供您可以使用的 RESTful 服务,而不是让用户侧向加载间隙而头疼。并非所有人都可以免费通过。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-16
    • 1970-01-01
    • 2015-01-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多