title: Connecting to existing Google Chromecast Session from Android (for generic remote control)
body: <p>I am creating a generic Chromecast remote control app. Most of the guts of the app are already created and I've managed to get Chromecast volume control working (by connecting to a Chromecast device along side another app that is casting - YouTube for example).</p>
  
  <p>What I've having difficult with is performing other media commands such as play, pause, seek, etc. </p>
  
  <p>Use case example:
  1. User opens YouTube on their android device and starts casting a video.
  2. User opens my app and connects to the same Chromecast device.
  3. Volume control from my app (works now)
  4. Media control (play, pause, etc) (does not yet work)</p>
  
  <p>I found the Cast api reference that explains that you can sendMessage(ApiClient, namespace, message) with media commands; however the "message" (JSON) requires the sessionId of the current application (Youtube in this case). I have tried the following, but the connection to the current application always fails; status.isSuccess() is always false:</p>
  
  <pre><code>Cast.CastApi
                      .joinApplication(mApiClient)
                      .setResultCallback(
                              new ResultCallback&lt;Cast.ApplicationConnectionResult&gt;() {
                                  @Override
                                  public void onResult(
                                          Cast.ApplicationConnectionResult result) {
                                      Status status = result.getStatus();
                                      if (status.isSuccess()) {
                                          ApplicationMetadata applicationMetadata = result
                                                  .getApplicationMetadata();
                                          sessionId = result.getSessionId();
                                          String applicationStatus = result
                                                  .getApplicationStatus();
                                          boolean wasLaunched = result
                                                  .getWasLaunched();
  
                                          Log.i(TAG,
                                                  "Joined Application with sessionId: "
                                                          + sessionId
                                                          + " Application Status: "
                                                          + applicationStatus);
  
                                      } else {
                                          // teardown();
                                          Log.e(TAG,
                                                  "Could not join application: "
                                                          + status.toString());
                                      }
                                  }
                              });
  </code></pre>
  
  <p>Is is possible to get the sessionId of an already running cast application from a generic remote control app (like the one I am creating)? If so, am I right in my assumption that I can then perform media commands on the connected Chromecast device using something like this:</p>
  
  <pre><code>JSONObject message = new JSONObject();
              message.put("mediaSessionId", sessionId);
              message.put("requestId", 9999);
              message.put("type", "PAUSE");
  
              Cast.CastApi.sendMessage(mApiClient,
              "urn:x-cast:com.google.cast.media", message.toString());
  </code></pre>
  
  <p>Update:
  I have tried the recommendations provided by @Ali Naddaf but unfortunately they are not working. After creating mRemoteMediaPlayer in onCreate, I also do requestStatus(mApiClient) in the onConnected callback (in the ConnectionCallbacks). When I try to .play(mApiClient) I get an IllegalStateException stating that there is no current media session. Also, I tried doing joinApplication and in the callback performed result.getSessionId; which returns null.</p>