首页 文章

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

提问于
浏览
0

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

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

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

...好像在几次停止,重启周期后,SpeechService没有正常关闭 .

My code: 我这样停止我的代码,我怀疑在我的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

    我设法通过在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();
                    }
                }
            }
    

相关问题