没有Google Apps的Android设备上的SpeechRecognizer

Rap*_*tor 9 android speech-recognition

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);
Run Code Online (Sandbox Code Playgroud)

其中CustomListener()工具RecognitionListener.

bra*_*all 5

我确定您知道以下大多数情况,但为了得到全面的答复:

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

Google RecognitionService打包在其Google'Now'应用程序中,您已经知道,该应用程序依赖于Google Play服务。

Vlingo RecognitionService在他们的S-Voice应用程序中,该应用程序仅公开预装在Samsung设备上-因此并不真正适用于您的问题,但由于我的评论较深,我在此提及。

在使用SpeechRecognizer之前,应始终使用静态帮助器方法:

if (SpeechRecognizer.isRecognitionAvailable(getApplicationContext())) {
    // initialise
   } else {
   // nope
   }
Run Code Online (Sandbox Code Playgroud)

方法文档所述

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

如果可以识别,则返回true,否则返回false

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

需要注意的是,Vlingo在这里将返回true,但实际上不会返回语音响应,由于某种原因它只会抛出ERROR_NETWORK。它很烦人。

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

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

任何空列表,将意味着没有提供者可用。

最后,如评论中所述,您始终可以高音检查并确保已安装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;
    }
}
Run Code Online (Sandbox Code Playgroud)

Google的包裹名称在哪里 com.google.android.googlequicksearchbox

最后,我确定您知道还有很多其他的语音识别提供程序可以提供RESTful服务,而不是让用户承受侧面的负载差距。并非所有人都是免费的。