Search code examples
androidchromecast

How to receive status updates from Cast Device


I've developed an integration with Chromecast for my Android Audio Streaming application. For the receiver, I'm using the Styled Media Receiver as described on the Chromecast developer page . Mostly everything is working fine, however, I'm not receiving onStatusUpdated() calls from the Cast device during Playback. When I start a media stream, it begins play and sends a status update with IDLE MediaStatus. Once it starts playing I receive a call to onStatusUpdated() with the PLAYING MediaStatus. After this, I'm not receiving any updates, which means I'm unable to update my application UI with the current Seek Position of the audio clip.

To work around this I'm basically running a Runnable every time I get the PLAYING MediaStatus. Here's the pseudocode:

public void onStatusUpdated() {
    ...
    if (mMediaStatus == MediaStatus.PLAYER_STATE_PLAYING) {
        mHandler.postDelayed(mUpdateProgressRunnable, 1000);
        ...

}

mUpdateProgressRunnable = new Runnable() {
    @Override
    public void run() {
        if (mRemoteMediaPlayer != null && mApiClient != null) {
            mRemoteMediaPlayer.requestStatus(mApiClient);
        }
    }
}

Is this the right way to do this? Why doesn't the Receiver just send updates on its own? This seems to work fine, but I'm surprised that I haven't been able to find any information online regarding the right way to do this.

Appreciate any help! Thanks.


Solution

  • You need to have a thread that will update the seek bar. But you don't need to explicitly do a requestStatus for the current position in the video. You should be calling

    RemoteMediaPlayer.getApproximateStreamPosition
    

    This is managed by the Cast SDK and is an efficient way of keeping up with the media playback progress on the receiver.