我们如何将录制的音频发送到语音识别器中?

时间:2021-02-01 02:57:15

标签: java android-studio speech-recognition speech-to-text mediarecorder

我是安卓开发的初学者。我正在构建一个自定义键盘应用程序,其中添加了麦克风键。此麦克风通过 MediaRecorder() 录制音频并以某种格式(如 3gp)保存文件。现在我想将此录制的音频发送到语音识别器,以便我可以将音频转换为文本。我在互联网上搜索过这个,但无法得到任何有效的答案。有人能告诉我我们怎么做吗?如果这是不可能的,我想以其他方式尝试,即,我想通过语音识别器录制并将语音转换为文本,但我也希望将录制的语音保存在存储或某个变量中,以便我可以使用它出于另一个目的。我找不到关于这两种可能性的答案。有人可以帮我解决这个问题吗? 录制音频并保存的代码(此应用没有任何活动)

if(!isMic_on) {
   isMic_on = !isMic_on;
   File dir = Environment.getExternalStorageDirectory();
   try {
       audiofile = File.createTempFile("sound", ".3gp", dir);
   } catch (IOException e) {
         Log.e(TAG, "external storage access error");
         return;
   }
   
   recorder = new MediaRecorder();
   recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                    

   recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
                    
   recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
   recorder.setOutputFile(audiofile.getAbsolutePath());
   try {
       recorder.prepare();
    } catch (IOException e) {
        e.printStackTrace();
    }

   recorder.start();
   Toast.makeText(EDMTKeyboard.this, "Recording Started", Toast.LENGTH_SHORT).show();
   }
else
{
  isMic_on = !isMic_on;
  recorder.stop();
  recorder.reset();
  recorder.release();
  recorder = null;
                    
  Toast.makeText(EDMTKeyboard.this, "Recording Stopped", Toast.LENGTH_SHORT).show();
}

我之前曾使用 sppechrecognizer 将语音转换为文本,但不知道如何获得录制的语音。这是语音识别器的代码。

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());
                    
                                //System.out.println(match);
                            }
                        }

                    }

       @Override
       public void onPartialResults(Bundle partialResults) {

                    }

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

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

  

0 个答案:

没有答案