你如何使用安卓的实时语音转文本?



在Android 4.1中,您可以使用键盘上的麦克风选项获得实时的文本转换。

我一直在寻找Android.spech的文档,试图找出如何实现应用程序的实时语音。但是,唯一有助于此的选项是" extra_partial_results"选项(每次我尝试使用它时,服务器都会忽略它)。

代码:

Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
        RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, "VoiceIME");
intent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true);
intent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS, 3000L);
mSpeaker.startListening(intent);

永远不会返回部分结果。

我知道这是可能的,因为键盘版本始终如一。有人知道如何吗?

在调用startListening之前,您需要注册onPartialResults -Callback。要注意的两件事要注意:

  • Android API未指定onPartialResults的捆绑包结构;
  • 并非每个语音识别器都支持此回调。

因此,您的代码将针对Google语音搜索。

mSpeaker.setRecognitionListener(new RecognitionListener() {
  ...
  public void onPartialResults(Bundle partialResults) {
    // WARNING: The following is specific to Google Voice Search
    String[] results = 
      partialResults.getStringArray("com.google.android.voicesearch.UNSUPPORTED_PARTIAL_RESULTS");
    updateTheUi(results);
  }
  ...
}

要在开源应用中看到此回调,请参见Babble:

  • Google Play:https://play.google.com/store/apps/details?id=be.lukin.android.babble
  • 源代码:https://github.com/lukin0110/babble/blob/master/android/andapp/src/src/be/lukin/android/android/babble/babbleactivity.java

如果您想在麦克风开启时出现实时部分结果,而说话者在说话时,您可能需要使用识别器和删除识别式服务来删除方法,以支持简单Android文本框与"麦克风"图标的事先选择,就像您在Android" Notes"示例应用程序中可以做的一样...

请参阅./samples/android-16/notepad/tests/src/com/com/example/android/notepad

此组合提供了像您实时看到的功能一样的功能,部分文本从言论结果中从服务器端" VoiceSearch"返回时,与" partial"回调的"识别器"不同。

许多评论指出,识别器不会向" OnpartialResults"发出回调。由于某种原因,Android 4.2似乎不支持使用JavaScript正常工作的"连续"语音识别模式。我在4.2上对"识别路标"接口的测试显示了数百个回调,以" onrmschanged"在卷事件上,但在" partialResult"事件上进行了零活动。在某个地方,这个回调丢失了吗?

对于JS解决方案,请安装Chrome-Beta版本25,然后进入此处

使用Android Notes应用程序。示例并从键盘预选麦克风图标,您可以做与上面的JS WebApp链接完全相同的事情。

,因为我们无法确定从部分结果回调的捆绑包的关键名称,请使用它来找出其内容:

public void onPartialResults(Bundle partialResults) {    
    String string = "Bundle{";
    for (String key : partialResults.keySet()) {
        string += " " + key + " => " + partialResults.get(key) + ";";
    } 
    Log.e("joshtag","onPartialResults"+string);
    //see the keynames in Logcat and extract partial reesults here
}

最新更新