2

我正在尝试同时录制音频和进行语音识别。他们每个人都单独工作,但在一起只有录音工作。

代码如下所示:

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 替代品。

4

0 回答 0