title: Google cast: IllegalStateException - Not connected to a device
body: <p>We are getting reports about a crash which seems to be caused by google cast SDK. We don't know how to reproduce it, just that it occurs in production.</p>
  
  <pre><code>'com.google.android.gms:play-services-cast-framework:11.6.2'
  </code></pre>
  
  <p>..and here is the report from crashlytics:</p>
  
  <pre><code>Fatal Exception: java.lang.IllegalStateException: Not connected to a device
     at com.google.android.gms.internal.zzbbc.zzaey(Unknown Source)
     at com.google.android.gms.internal.zzbbc.isMute(Unknown Source)
     at com.google.android.gms.cast.Cast$CastApi$zza.isMute(Unknown Source)
     at com.google.android.gms.cast.framework.CastSession.isMute(Unknown Source)
     at com.google.android.gms.internal.zzazf.zzaee(Unknown Source)
     at com.google.android.gms.internal.zzazf.onMediaStatusUpdated(Unknown Source)
     at com.google.android.gms.cast.framework.media.uicontroller.UIMediaController.zzaed(Unknown Source)
     at com.google.android.gms.cast.framework.media.uicontroller.UIMediaController.onStatusUpdated(Unknown Source)
     at com.google.android.gms.cast.framework.media.zzn.onStatusUpdated(Unknown Source)
     at com.google.android.gms.internal.zzbbw.onStatusUpdated(Unknown Source)
     at com.google.android.gms.internal.zzbbw.zza(Unknown Source)
     at com.google.android.gms.internal.zzbbw.zzfg(Unknown Source)
     at com.google.android.gms.cast.framework.media.RemoteMediaClient.onMessageReceived(Unknown Source)
     at com.google.android.gms.internal.zzbbi.run(Unknown Source)
     at android.os.Handler.handleCallback(Handler.java:751)
     at android.os.Handler.dispatchMessage(Handler.java:95)
     at android.os.Looper.loop(Looper.java:154)
     at android.app.ActivityThread.main(ActivityThread.java:6776)
     at java.lang.reflect.Method.invoke(Method.java)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
  </code></pre>
  
  <p>Does anyone have any insight on this? Everything in the logs seems to be internal things, nothing on our app.</p>
  
  <p>Edit: This is the code from com.google.android.gms.internal where the crash occurs. My best bet is that there is a race condition where the app loses connection just before the call to <strong>var1.isMute()</strong> which in turn throws the Exception "Not connected to a device"</p>
  
  <pre><code>protected final void zzaer() {
      CastSession var1;
      if((var1 = CastContext.getSharedInstance(this.zzbhi).getSessionManager().getCurrentCastSession()) != null &amp;&amp; var1.isConnected()) {
          RemoteMediaClient var2;
          if((var2 = this.getRemoteMediaClient()) != null &amp;&amp; var2.hasMediaSession()) {
              this.zzfbo.setEnabled(true);
          } else {
              this.zzfbo.setEnabled(false);
          }
  
          if(var1.isMute()) { // This row causes the crash
              this.zzaz(true);
          } else {
              this.zzaz(false);
          }
      } else {
          this.zzfbo.setEnabled(false);
      }
  }
  </code></pre>
  
  <p>Edit: I've filed the issue to google: <a href="https://issuetracker.google.com/issues/72880677" rel="nofollow noreferrer">https://issuetracker.google.com/issues/72880677</a></p>
  
  <p>Edit: Creating custom expanded controls and making sure that mute button is not binded to the views seems to mitigate the crashes.</p>