java - 如何在我的应用程序中实现语音转文本

标签 java android

我正在构建一个将语音转换为文本的应用程序,在我的代码中一切看起来都很好,直到它到达 protected void onActivityResult 方法来处理结果,它会生成一个错误,说 onActivityResult 是一个变量,然后如果我删除访问修饰符,它将其视为一种方法,然后在参数中生成另一个错误,表示预期标识符和 token 丢失 任何帮助将不胜感激。

我的代码

 private static final int SPEECH_REQUEST_CODE = 100;

// Create an intent that can start the Speech Recognizer activity
private void displaySpeechRecognizer() {
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
            RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
// Start the activity, the intent will be populated with the speech text
    startActivityForResult(intent, SPEECH_REQUEST_CODE);
}

// This callback is invoked when the Speech Recognizer returns.
// This is where you process the intent and extract the speech text from the intent.
@Override
protected void onActivityResult(int requestCode, int resultCode,
        Intent data) {
super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == SPEECH_REQUEST_CODE && resultCode == RESULT_OK) {
        List<String> results = data.getStringArrayListExtra(
                RecognizerIntent.EXTRA_RESULTS);
       Output.setText(results.get(0));

    }

}

最佳答案

这样做:

    private void displaySpeechRecognizer() {
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
            RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
    intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
            getString(R.string.speech_prompt));
    try {
        startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
    } catch (ActivityNotFoundException a) {
        Toast.makeText(getApplicationContext(),
                getString(R.string.speech_not_supported),
                Toast.LENGTH_SHORT).show();
    }
}

在 onActivityResult 中:

  @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    switch (requestCode) {
        case REQ_CODE_SPEECH_INPUT: {
            if (resultCode == RESULT_OK && null != data) {

                ArrayList<String> result = data
                        .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                txtSpeechInput.setText(result.get(0));
            }
            break;
        }

    }
}

在 onCreate 中:

//Button you craete to start speech recognition    
Button btnSpeak = (ImageButton) findViewById(R.id.btnSpeak);

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

        btnSpeak.setOnClickListener(new View.OnClickListener() {

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

关于java - 如何在我的应用程序中实现语音转文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61499023/

相关文章:

Java解析文本文件

android - 工作区构建卡住怎么办?

android - Galaxy Nexus 上的 AdMob - Android 应用程序自行关闭

android - PreferenceActivity 未显示(黑屏)

android - 以 MHz 为单位查找 Android 设备的处理器速度

java - 将图像旋转到 'point'到鼠标位置

java - 将 bigint postgres 数据类型隐式转换为 Java Long

java - 是否可以在 Orika 中将列表索引指定为可选?

java - 如何将 HTML 表数据绑定(bind)到 Spring Controller 中的 java 对象?

android - 两个类成员映射到新的最终类