title: Google Speech Cloud error on Android: OUT_OF_RANGE: Exceeded maximum allowed stream duration of 65 seconds
body: <p><strong>First:</strong> 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: <a href="https://github.com/GoogleCloudPlatform/android-docs-samples/tree/master/speech" rel="nofollow noreferrer">https://github.com/GoogleCloudPlatform/android-docs-samples/tree/master/speech</a>
  The app works fairly well, I get ASR results and show them onscreen as the user speaks, Siri style.</p>
  
  <p><strong>The problem:</strong>
  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: </p>
  
  <pre><code>io.grpc.StatusRuntimeException: OUT_OF_RANGE: Exceeded maximum allowed stream duration of 65 seconds.
  </code></pre>
  
  <p>...as if the SpeechService was not shut down properly after several stop, restart cycles.</p>
  
  <p><strong>My code:</strong>
  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):</p>
  
  <pre><code>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;
          }
      }
  </code></pre>
  
  <p>Am I stopping the service properly in order to avoid the 65 Second Limit error? Any recommendations?</p>