Android 上的 Google Speech Cloud 错误:OUT_OF_RANGE:超过了 65 秒的最大允许流持续时间 [英] Google Speech Cloud error on Android: OUT_OF_RANGE: Exceeded maximum allowed stream duration of 65 seconds

查看:83
本文介绍了Android 上的 Google Speech Cloud 错误:OUT_OF_RANGE:超过了 65 秒的最大允许流持续时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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.

...就好像 SpeechService 在多次停止、重启循环后没有正确关闭一样.

...as if the SpeechService was not shut down properly after several stop, restart cycles.

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

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

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

Am I stopping the service properly in order to avoid the 65 Second Limit error? Any recommendations?

推荐答案

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

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

这篇关于Android 上的 Google Speech Cloud 错误:OUT_OF_RANGE:超过了 65 秒的最大允许流持续时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆