Android上的Google Speech Cloud错误:OUT_OF_RANGE:超过允许的最长流时长为65秒

时间:2017-09-19 10:04:46

标签: java android multithreading speech-recognition google-speech-api

首先:我已经知道使用此API对连续语音识别流进行了65秒的限制。我的目标不是延长65秒。 我的应用: 它使用谷歌的流式语音识别,我的代码基于这个例子:https://github.com/GoogleCloudPlatform/android-docs-samples/tree/master/speech 该应用程序运行良好,我得到ASR结果,并在用户说话时在屏幕上显示它们,Siri风格。

问题: 在我的应用程序上点击几个ASR按钮之后出现问题,停止并重新启动ASR SpeechService有点不可靠。最终,我收到了这个错误:

io.grpc.StatusRuntimeException: OUT_OF_RANGE: Exceeded maximum allowed stream duration of 65 seconds.

...好像在几次停止,重启循环后,SpeechService没有正常关闭。

我的代码: 我像这样停止我的代码,我怀疑在我的StopStreamingASR方法的实现中的问题。我在一个单独的线程上运行它,因为我相信它可以提高性能(希望我没错):

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;
        }
    }

我是否正确停止服务以避免65秒限制错误?有什么建议吗?

1 个答案:

答案 0 :(得分:0)

我设法通过在streamObserver中的OnNext()方法中添加finishRecognizing()来解决它:

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();
                }
            }
        }