问题
First: I already know there is a 65 second limit on continuous speech recognition streaming with this API. My goal is NOT to extend those 65 seconds. My app: It uses Google's streaming Speech Recognition, I based my code on this example: https://github.com/GoogleCloudPlatform/android-docs-samples/tree/master/speech The app works fairly well, I get ASR results and show them onscreen as the user speaks, Siri style.
The problem: My problem comes after tapping the ASR button on my app several, stopping and restarting the ASR SpeechService is a bit unreliable. Eventually, I get this error:
io.grpc.StatusRuntimeException: OUT_OF_RANGE: Exceeded maximum allowed stream duration of 65 seconds.
...as if the SpeechService was not shut down properly after several stop, restart cycles.
My code: I stop my code like this, I suspect the problem somewhere in the implementation of my StopStreamingASR method. I run it on a separate Thread because I believe it can improve performance (hoping I'm not wrong):
static void StopStreamingASR(){
loge("stopGoogleASR_API");
//stopMicGlow();
//Run on separate thread to keep UI thread light.
Thread thread = new Thread() {
@Override
public void run() {
if(mSpeechService!=null) {
mSpeechService.finishRecognizing();
stopVoiceRecorder();
// Stop Cloud Speech API
if(mServiceConnection!=null) {
try {
app.loge("CAUTION, attempting to stop service");
try {
mSpeechService.removeListener(mSpeechServiceListener);
//original mActivity.unbindService(mServiceConnection);
app.ctx.unbindService(mServiceConnection);
mSpeechService = null;
}
catch(Exception e){
app.loge("Service Shutdown exception: "+e);
}
}
catch(Exception e){
app.loge("CAUTION, attempting to stop service FAILED: "+e.toString());
}
}
}
}
};
thread.start();
}
private static void stopVoiceRecorder() {
loge("stopVoiceRecorder");
if (mVoiceRecorder != null) {
mVoiceRecorder.stop();
mVoiceRecorder = null;
}
}
Am I stopping the service properly in order to avoid the 65 Second Limit error? Any recommendations?
回答1:
I manage to solve it by adding the finishRecognizing() inside the OnNext() method from the streamObserver:
public void onNext(StreamingRecognizeResponse response) {
String text = null;
boolean isFinal = false;
if (response.getResultsCount() > 0) {
final StreamingRecognitionResult result = response.getResults(0);
isFinal = result.getIsFinal();
if (result.getAlternativesCount() > 0) {
final SpeechRecognitionAlternative alternative = result.getAlternatives(0);
text = alternative.getTranscript();
}
}
if (text != null) {
for (Listener listener : mListeners) {
listener.onSpeechRecognized(text, isFinal);
}
if(isFinal)
{
finishRecognizing();
}
}
}
来源:https://stackoverflow.com/questions/46297592/google-speech-cloud-error-on-android-out-of-range-exceeded-maximum-allowed-str