【问题标题】:Speech Recognition - get all languages supported - Android语音识别 - 支持所有语言 - Android
【发布时间】:2016-05-26 16:37:39
【问题描述】:

我很难让它发挥作用。我只是想找出语音识别支持的所有可用语言,以及使用它的代码。

RecognizerIntent.EXTRA_SUPPORTED_LANGUAGES

这应该将 G 的语音识别服务器上的所有可用语言作为一个数组报告回来。不幸的是,我什至无法让它吐出一个。我认为我主要只是在使用 BroadcastReceiver 时遇到问题。

package com.thatll.ddo;

import java.util.ArrayList;
import java.util.logging.Logger;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.util.Log;
import android.widget.Toast;

public class RecogtestActivity extends Activity {
    private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;



/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Log.i("Log", "startVoiceRecognitionActivity");
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "speech recognition demo");

    startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) {

     Intent intent = new Intent(RecognizerIntent.ACTION_GET_LANGUAGE_DETAILS);
     LangBroadcastReceiver myBroadcastReceiver = new LangBroadcastReceiver(this, data.getStringArrayListExtra(RecognizerIntent.EXTRA_SUPPORTED_LANGUAGES));
     sendOrderedBroadcast(intent, null, myBroadcastReceiver, null, Activity.RESULT_OK, null, null);

  } else {
     Toast.makeText(getApplicationContext(), "Voice recognition failed.", Toast.LENGTH_LONG).show();
  }
  super.onActivityResult(requestCode, resultCode, data);
}



/**
 * After a voice recognition is performed, need to sent a broadcast to
 * request the language used. This BroadcastReceiver gets the response and
 * then processes the original recognisedText from the
 * ACTION_RECOGNIZE_SPEECH Intent.
 * 
 */
public class LangBroadcastReceiver extends BroadcastReceiver {
  ArrayList<String> recognisedText;
  Activity parentActivity;

  /**
   * Store these for later use...
   * @param activity
   * @param arrayList
   */
  LangBroadcastReceiver(Activity activity, ArrayList<String> arrayList) {
     recognisedText = arrayList;
     parentActivity = activity;
  }

  @Override
  public void onReceive(Context context, Intent intent) {
     Bundle results = getResultExtras(true);
     String lang = results.getString(RecognizerIntent.EXTRA_SUPPORTED_LANGUAGES);
     Log.d("log", "MyBroadcastReceiver: Got 'EXTRA_LANGUAGE_PREFERENCE' = " + lang);
     // now handle the recognisedText with the known language.

     ArrayList<CharSequence> hints = getResultExtras(true)
             .getCharSequenceArrayList(
                     RecognizerIntent.EXTRA_SUPPORTED_LANGUAGES);

     Log.d("log", "MyBroadcastReceiver: Got 'EXTRA_LANGUAGE_PREFERENCE' =   " + hints);

  }

}}

我想知道为什么这不起作用,但在这一点上,如果我能获得当前支持的语言的完整列表,我会很高兴。尝试了各种不同的方法,现在它开始让我烦恼。

感谢您的帮助

【问题讨论】:

    标签: android broadcastreceiver speech-recognition


    【解决方案1】:

    请在此处查看我的回答,我描述了如何正确执行RecognizerIntent.ACTION_GET_LANGUAGE_DETAILS

    How to set the language in speech recognition on android?

    【讨论】:

    • 谢谢你,做到了。由于主题是设置语言,因此在搜索时一定错过了这一点。
    【解决方案2】:

    您可能对这个小图书馆感兴趣:https://github.com/Kaljurand/speechutils

    具体来说,RecognitionServiceManager 类会查询所有已安装的语音识别服务,了解它们支持的所有语言。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多