Java – How to get play and stop event callbacks from chromecast android

How to get play and stop event callbacks from chromecast android… here is a solution to the problem.

How to get play and stop event callbacks from chromecast android

I want to get the callback event and perform some functions when the chromecast audio changes the playback mode (play/stop).

How can I get the app to play/stop an event so that I can do my job on that event.

Please check the following logic that I have implemented.

 private void setupCastListener() {
        mSessionManagerListener = new SessionManagerListener<CastSession>() {

@Override
            public void onSessionEnded(CastSession session, int error) {
                onApplicationDisconnected();
            }

@Override
            public void onSessionResumed(CastSession session, boolean wasSuspended) {
                onApplicationConnected(session);
            }

@Override
            public void onSessionResumeFailed(CastSession session, int error) {
                onApplicationDisconnected();
            }

@Override
            public void onSessionStarted(CastSession session, String sessionId) {
                onApplicationConnected(session);
            }

@Override
            public void onSessionStartFailed(CastSession session, int error) {
                onApplicationDisconnected();
            }

@Override
            public void onSessionStarting(CastSession session) {
            }

@Override
            public void onSessionEnding(CastSession session) {
            }

@Override
            public void onSessionResuming(CastSession session, String sessionId) {

if(mCastSession!=null && isChromeCastConnected){
                  try {
                      if (session.isMute()) {
                          mStopPlayButton.setImageResource(R.drawable.ic_play);
                          isChromeCastPlay = false;
                          mCastSession.setMute(!mCastSession.isMute());
                      } else {
                          mStopPlayButton.setImageResource(R.drawable.ic_stop);
                          isChromeCastPlay = true;
                          mCastSession.setMute(!mCastSession.isMute());
                      }
                  } catch (Exception e) {
                      e.printStackTrace();
                  }
              }
            }

@Override
            public void onSessionSuspended(CastSession session, int reason) {
            }
        };
    }

Please let me know. Thanks

Solution

Finally, under MediaControlIntent, find the callbacks for remote media playback and pause modes.

Remote Playback Routes

mMediaRouter = MediaRouter.getInstance(this);
mSelector = new MediaRouteSelector.Builder()
            .addControlCategory(MediaControlIntent.CATEGORY_REMOTE_PLAYBACK)
            .build();

Related Problems and Solutions