首页 > 解决方案 > 当我在 android studio 中使用 SpeechRecognizer 时,哪个函数会返回录制的音频?

问题描述

我正在构建一个自定义键盘应用程序(在 android studio 中),我在其中添加了一个麦克风键。我想将录制的语音转换为文本,所以我使用了语音识别器。但我也想要录制的音频。我希望我的键盘应用程序在像whatsapp这样的聊天应用程序中记录音频时,它应该将该音频作为消息发送。当我使用语音识别器时,有人可以帮助我如何获取录制的音频。我只需要使用语音识别器,因为我需要它用于其他目的。(ps:我的应用程序没有任何活动)这是用于录制语音并转换为文本的代码。

Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, "com.example.keyboard");

SpeechRecognizer recognizer = SpeechRecognizer.createSpeechRecognizer(this.getApplicationContext());

RecognitionListener listener = new RecognitionListener() {

    @Override
    public void onReadyForSpeech(Bundle params) {
        Toast.makeText(EDMTKeyboard.this, "Recording Started", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onBeginningOfSpeech() {
        Toast.makeText(EDMTKeyboard.this, "Recording Started", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onRmsChanged(float rmsdB) {

    }

    @Override
    public void onBufferReceived(byte[] buffer) {

    }

    @Override
    public void onEndOfSpeech() {

    }

    @Override
    public void onError(int error) {
        System.err.println("Error listening for speech: " + error);
        Toast.makeText(EDMTKeyboard.this, "ERROR-"+error, Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onResults(Bundle results) {

        Bundle bundle = intent.getExtras();

        ArrayList<String> voiceResults = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);

        if (voiceResults == null) {
            System.out.println("No voice results");
        } else {
            System.out.println("Printing matches: ");
            for (String match : voiceResults) {
                Toast.makeText(EDMTKeyboard.this, match, Toast.LENGTH_SHORT).show();
                ic.commitText(match,match.length());
                str = str + match;
                I=I+match.length()+1;
                //System.out.println(match);
            }
        }
    }

    @Override
    public void onPartialResults(Bundle partialResults) {

    }

    @Override
    public void onEvent(int eventType, Bundle params) {

    }
};
recognizer.setRecognitionListener(listener);
recognizer.startListening(intent);

我在stackoverflow中检查了与此类似的问题,但给出的答案目前不起作用(可能是android studio删除了这些功能)。

标签: javaandroidandroid-studiospeech-recognition

解决方案


推荐阅读