你如何使用Android的实时语音文本?

时间:2012-12-22 05:17:05

标签: android

在Android 4.1中,您可以使用keyboard上的麦克风选项获取实时语音转换为文本。

我一直在查看android.speech的文档,试图找出如何为应用程序实现文本的实时语音。但是,唯一可以促成这一点的选择是" 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);

永远不会返回部分结果。

我知道这是可能的,因为键盘版本能够始终如一。谁知道怎么做?

3 个答案:

答案 0 :(得分:4)

在致电startListening之前,您需要注册onPartialResults - 回拨。需要注意的两件重要事情:

  • 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:

答案 1 :(得分:0)

如果您希望在麦克风打开时显示实时部分结果,而扬声器正在讲话,您可能希望使用recognizerIntent和drop recognitionService删除方法,而选择简单的Android文本框并结合先前的选择你可以在android'notes'示例应用程序中执行'mic'图标...

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

这个组合提供了一个功能,就像你实时看到的部分文本语音结果一样,它们从服务器端“voiceSearch”返回,这与“识别器”在某种程度上与“部分”回调有所不同。

许多评论声明识别器内容不会触发对'onPartialResults'的回调。出于某种原因,android 4.2似乎不支持使用javascript正常工作的“连续”speechRecognition模式。我在4.2上对'RecognitionListener'接口的测试显示了对卷事件的'onRmsChanged'的数百个回调,但对'partialResult'事件的调用为零。在某个地方,这个回调会丢失?

对于js解决方案,安装chrome-beta版本25并转到here

使用android notes app。从键盘上取样并预先选择麦克风图标,你可以做与上面的JS webapp链接完全相同的事情。

答案 2 :(得分:0)

由于我们无法确定来自部分结果回调的Bundle的密钥名称,因此使用它来查找其内容:

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
}
相关问题