【问题标题】:speech to text api other language android语音到文本 api 其他语言 android
【发布时间】:2014-11-28 19:39:07
【问题描述】:

我开发了可以识别普通话语音,然后产生文本的安卓应用程序。但我找不到如何做到这一点。谁能给我其他语言(普通话、法国等)语音识别的示例代码?

public class MainActivity extends Activity {

private TextView txtSpeechInput;
private ImageButton btnSpeak;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    txtSpeechInput = (TextView) findViewById(R.id.txtSpeechInput);
    btnSpeak = (ImageButton) findViewById(R.id.btnSpeak);

    // hide the action bar
    getActionBar().hide();

    btnSpeak.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            listen();
        }
    });

}



private static int SR_CODE = 123;

/**
 * Initializes the speech recognizer and starts listening to the user input
 */
private void listen()  {
        Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        //Specify language
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.SIMPLIFIED_CHINESE);
        // Specify language model
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        // Specify how many results to receive
        intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 5);
        // Start listening
        startActivityForResult(intent, SR_CODE);
    }

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == SR_CODE && resultCode == RESULT_OK) {
        if (data != null) {
            // Retrieves the best list SR result
            ArrayList<String> nBestList = data
                    .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
            String bestResult = nBestList.get(0);
            Toast.makeText(getApplicationContext(), bestResult,
                    Toast.LENGTH_LONG).show();
        } else {
            // Reports error in recognition error in log
            Log.e("Log", "Recognition was not successful");
        }
    }

}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
}

【问题讨论】:

  • 提供的解决方案有效吗?
  • 对不起,我刚刚打开了这个问题。它就像我在下面的帖子一样工作。我不知道为什么 {locale.SIMPLEFIED_CHINESE} 不能产生中文单词。不过谢谢你帮助我。

标签: java android google-api speech-to-text


【解决方案1】:
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "zh");
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "zh");
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, "zh");
intent.putExtra(RecognizerIntent.EXTRA_SUPPORTED_LANGUAGES, "zh");
intent.putExtra(RecognizerIntent.EXTRA_ONLY_RETURN_LANGUAGE_PREFERENCE,"zh");
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, "zh");
intent.putExtra(RecognizerIntent.EXTRA_RESULTS, "zh");

【讨论】:

    【解决方案2】:

    调用识别器时可以设置RecognizerIntent.EXTRA_LANGUAGE

    所以简体中文的例子是:

    private static int SR_CODE = 123;
    
    
    /**
         * Initializes the speech recognizer and starts listening to the user input
         */
        private void listen()  {
            Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
            //Specify language
            intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.SIMPLIFIED_CHINESE)
            // Specify language model
            intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
            // Specify how many results to receive
            intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 5);
            // Start listening
            startActivityForResult(intent, SR_CODE);
        }
    
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (requestCode == SR_CODE && resultCode == RESULT_OK)  {
                    if(data!=null) {
                    //Retrieves the best list SR result
                    ArrayList<String> nBestList = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                   String bestResult = nBestList.get(0);
                   Toast.makeText(getApplicationContext(), bestResult, Toast.LENGTH_LONG).show;              
                }else {         
                    //Reports error in recognition error in log
                    Log.e(LOGTAG, "Recognition was not successful");
                }
    
        }
    

    【讨论】:

    • 在onActivityResult回调中使用的整数常量。我已经更新了我的答案。如果问题没有解决,请告诉我
    • 我已经完成了你的代码。但它总是以英文显示。我已经更新了我的问题代码,就像你的代码一样。但是非常感谢你帮助我
    • 您是否尝试过其他语言。示例:“fr_FR”而不是 Locale.SIMPLIFIED_CHINESE。请注意,只有在 Google 支持该语言时它才会运行。另请注意,从 4.4 开始,Android 允许在 Settings 中选择多种语言,并且会始终尝试根据系统设置“猜测”语言并完全忽略 EXTRA_LANGUAGE 额外
    猜你喜欢
    • 2018-05-08
    • 2012-12-16
    • 2019-11-05
    • 2015-06-16
    • 2023-03-03
    • 1970-01-01
    • 2017-10-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多