问题
I'm trying to record audio and do speech recognition at the same time. Each of them works separately, but together only the recording works.
The code looks like that:
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);
}
When both methods are called, the onReadyForSpeech callback is called but nothing comes through. When only startRecognition() is called, speech recognition works fine.
I'm guessing it's because the speech recognizer is also using the buffer from the the microphone, but I wonder how this issue can be worked around?
EDIT: I'm not looking to use cloud API or any other non-offline API (as suggested in other similar question). Also, taking the FLAC approach may lose the ability to get partial transcription results. I'm still looking at using, but would prefer a more standard non-jni alternative if possible.
来源:https://stackoverflow.com/questions/47001034/recording-and-speech-recognition-at-the-same-time-with-speechrecognizer-and-medi