使用SpeechRecognizer和MediaRecorder同时录制和语音识别

时间:2017-10-29 14:00:29

标签: android speech-recognition mediarecorder sfspeechrecognizer

我试图录制音频并同时进行语音识别。它们中的每一个都是分开工作的,但只有录音一起工作。

代码如下:

private SpeechRecognizer sr;
private MediaRecorder recorder;

private void startRecording() throws IOException {
    recorder = new MediaRecorder();
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    recorder.setOutputFile("/dev/null");
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

    recorder.prepare();
    recorder.start();
}

private void startRecognition() {
    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_MAX_RESULTS, 5);
    intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getPackageName());

    sr = SpeechRecognizer.createSpeechRecognizer(this);
    sr.setRecognitionListener(this);
    sr.startListening(intent);
}

调用这两个方法时,会调用onReadyForSpeech回调,但不会发生任何操作。当只调用startRecognition()时,语音识别工作正常。

我猜它是因为语音识别器也在使用麦克风的缓冲区,但我想知道这个问题是如何解决的?

编辑:我不打算使用云API或任何其他非离线API(如其他类似问题所示)。此外,采用FLAC方法可能会失去获得部分转录结果的能力。我仍然在考虑使用,但如果可能的话,我会更喜欢更标准的非jni替代方案。

0 个答案:

没有答案