如果Android的SpeechRecognizer无法工作,可能是语言包的问题。以下是一个代码示例来解决这个问题:
Intent installData = new Intent(RecognizerIntent.ACTION_GET_LANGUAGE_DETAILS);
sendOrderedBroadcast(installData, null, new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Bundle results = getResultExtras(true);
if (results.containsKey(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE)) {
// 语言包已安装
} else {
// 语言包未安装,尝试安装
Intent installIntent = new Intent(RecognizerIntent.ACTION_INSTALL_LANGUAGE_PACK);
startActivity(installIntent);
}
}
}, null, Activity.RESULT_OK, null, null);
SpeechRecognizer recognizer = SpeechRecognizer.createSpeechRecognizer(context);
recognizer.cancel();
recognizer.destroy();
recognizer = SpeechRecognizer.createSpeechRecognizer(context);
通过检查权限、安装语言包和重置语音识别引擎,你应该能够解决Android的SpeechRecognizer无法工作的问题。