java - Android在实现RecognitionListener时获取Intent

标签 java android android-intent android-bundle

this answer by Iftah 中所述我可以通过从 Intent 中获取 Uri 来获取 android 中语音识别记录的音频:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    // the recording url is in getData:
    Uri audioUri = data.getData();
}

这里的 Intent data 正是我想要的,没有问题。

这一切都完美无缺,但是该解决方案会提示用户何时说话,我不希望这样做,所以我让我的 Activity 实现 RecognitionListener:

public class MainActivity extends AppCompatActivity implements RecognitionListener {

    private SpeechRecognizer speech = null;
    private Intent recognizerIntent;

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

        recognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "sv_SE");
        recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, "sv_SE");
        recognizerIntent.putExtra("android.speech.extra.GET_AUDIO_FORMAT", "audio/AMR");
        recognizerIntent.putExtra("android.speech.extra.GET_AUDIO", true);

        speech = SpeechRecognizer.createSpeechRecognizer(this);
        speech.setRecognitionListener(this);
        speech.startListening(recognizerIntent);
    }

    @Override
    public void onReadyForSpeech(Bundle params) {
        Log.d("Debug", "On ready for speech");
    }

    @Override
    public void onBeginningOfSpeech() {
        Log.d("Debug", "On beggining of speech");
    }

    @Override
    public void onRmsChanged(float rmsdB) {
        Log.d("Debug", "Rsm changed");
    }

    @Override
    public void onBufferReceived(byte[] buffer) {
        Log.d("Debug", "Buffer recieved");
    }

    @Override
    public void onEndOfSpeech() {
        Log.d("Debug", "On end of speech");
    }

    @Override
    public void onError(int error) {
        Log.d("Debug", "Error");
    }

    @Override
    public void onPartialResults(Bundle partialResults) {
        Log.d("Debug", "On partial result");
    }

    @Override
    public void onEvent(int eventType, Bundle params) {
        Log.d("Debug", "On event");
    }

    @Override
    public void onResults(Bundle results) {
        Log.d("Debug", "On result");
    }
}

这绕过了提示,但我不知道如何像第一个示例中那样获取 Uri,因为这里:

@Override
    public void onResults(Bundle results) {
        Log.d("Debug", "On result");
        // The results bundle don't contain the URI!
    }

我得到一个不包含 Intent 或 Uri 的 Bundle results。我已尝试查看 bundle 中的所有键,但不存在 URI 或 Intent,我也尝试过 getIntent() 但没有返回任何内容。

感谢任何帮助或朝着正确的方向前进。

最佳答案

结果应该与键 SpeechRecognizer.RESULTS_RECOGNITION bundle 在一起。

@Override
public void onResults(Bundle results) {
    if (results != null && results.containsKey(SpeechRecognizer.RESULTS_RECOGNITION)) {
      List<String> resultsList = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
      // most likely result is resultsList.get(0)
    } else {
      // no results
    }

}

有关 SpeechRecognizer 的详细说明,请参阅 Android 开发人员文档。这种获取语音识别结果的方法也适用于#onPartialResults

中的部分结果

关于java - Android在实现RecognitionListener时获取Intent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35337153/

相关文章:

java - 有没有更快的方法来获取屏幕截图中的颜色?

java - Retrofit + Instagram API 在请求访问 token 时返回错误 400

Android:定义 onDraw Canvas 大小

android - 出现软键盘时布局不会向上推

android - 如何在android中使用Uri.Parse解析 '#' simbol

java - 如何闯入MULE中的流程中间并将消息返回给客户端..?

java - 执行器服务中的依赖线程 - Java

android - 为什么 Activity A在 Activity B被销毁时开始

android - 有没有办法从选择器对话框中排除某些应用程序?

Java 小程序适用于 Oracle JRE 7u5,不适用于 7u6