当前位置: 首页 > 面试题库 >

语音到文本api其他语言android

梅庆
2023-03-14
问题内容

我开发了可以识别普通话语音,然后生成文本的android应用程序。但是我找不到怎么做。有人可以给我示例其他语言(普通话,法国等)的语音识别代码吗?

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

问题答案:

您可以在调用识别器时设置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");
            }

    }


 类似资料:
  • 我似乎在这上面找不到任何东西。iOS7中是否有任何Siri类或API允许您进行文本到语音转换?我所要做的就是如下所示: 然后让Siri从我的应用程序中说出来。 看来我们应该有能力做到这一点,不是吗?似乎是一件微不足道的事情。

  • 使用Microsoft语音API转录中/大型音频文件(每个文件约6-10分钟)的最佳方式是什么?比如批量音频文件转录? 我使用了https://docs.microsoft.com/en-us/azure/cognitive-services/speech-service/speech-to-text-sample中提供的代码,用于连续转录语音,但它在某个时候停止转录。转录有任何限制吗?我只使用免

  • Android谷歌语音转文本SDK,语音录制由SDK控制。我需要将其设置为手动按钮,用于启动和停止语音录制,以便将语音转换为文本。例如:当单击按钮开始语音识别时,它会继续录制音频,直到单击停止按钮。但在android SDK中,它会自动停止录制并将录制的音频传递给处理。

  • 当使用缺乏原生Pact支持的语言来写服务提供者时,你仍然可以使用通用的Pact提供者端验证工具来验证是否满足契约。 通用Pact提供者验证 下面的设置简化了任何语言的Pact提供者端的验证过程。 特性: 验证发布到Pact Broker的Pact文件 在开发环境验证供测试用的本地Pact*.json文件 安装有Ruby环境以及sane的预先配置的Docker镜像,缺省为src / Rakefile

  • 我有一个代码,以前是有效的,但由于某种原因,它突然停止了工作,我正在尝试用希伯来语进行语音识别,但似乎几天前它才开始用英语进行语音识别。 这是我的代码 test\u voice\u recognitiona是我的RecognitionListener类名的名称。 代码运行良好,但出于某种原因,它一直在用英语听。 我做错了什么? 顺便说一句,我用谷歌对话框尝试了更简单的代码,它很管用。 也许是Goo

  • 我跟着这首短裙:https://jbinformatique.com/2018/02/16/android-speech-to-text-api-google-tutoriel/ 它工作得很好!它使用android.speech.识别意图包,它是免费的,它可以在没有互联网的情况下工作,正如这里提到的: Android语音到文本API(识别器意图)和Google Cloud Speech API之间