speech to text api other language android

和自甴很熟 提交于 2019-12-01 01:42:35

You can set RecognizerIntent.EXTRA_LANGUAGE when you call the recognizer.

So an example in simplified chinese will be:

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");
            }

    }
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");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!