b.lyte
b.lyte

Reputation: 6747

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.

Upvotes: 1

Views: 2282

Answers (2)

Robert Rowntree
Robert Rowntree

Reputation: 6289

https://github.com/googlecast/CastCompanionLibrary-android/blob/master/src/com/google/sample/castcompanionlibrary/cast/VideoCastManager.java

line 851 of that CCL class has an implementation of Cast.Listener that can be used to react to status events signaled on the remote player. I think you have to put the CCL into debug mode to get logger output like the following that occurs during "PLAY":

D/ccl_MyVideoCastManager( 2765): RemoteMediaPlayer::onStatusUpdated() is reached
D/ccl_MyVideoCastManager( 2765): onRemoteMediaPlayerStatusUpdated() reached 2 0
I/System.out( 2765): status: playing
D/ccl_MyVideoCastManager( 2765): updateMiniControllersVisibility() reached with visibility: true
D/ccl_VideoCastControlle( 2765): onRemoteMediaPlayerStatusUpdated(), status: 2
D/ccl_VideoCastNotificat( 2765): onRemoteMediaPlayerMetadataUpdated() reached with status: 2
D/ccl_MyVideoCastManager( 2765): RemoteMediaPlayer::onStatusUpdated() is reached
D/ccl_MyVideoCastManager( 2765): onRemoteMediaPlayerStatusUpdated() reached 4 0
I/System.out( 2765): status: buffering
D/ccl_MyVideoCastManager( 2765): updateMiniControllersVisibility() reached with visibility: true
D/ccl_VideoCastControlle( 2765): onRemoteMediaPlayerStatusUpdated(), status: 4
D/ccl_VideoCastNotificat( 2765): onRemoteMediaPlayerMetadataUpdated() reached with status: 4
D/ccl_MyVideoCastManager( 2765): RemoteMediaPlayer::onStatusUpdated() is reached
D/ccl_MyVideoCastManager( 2765): onRemoteMediaPlayerStatusUpdated() reached 2 0
I/System.out( 2765): status: playing
D/ccl_MyVideoCastManager( 2765): updateMiniControllersVisibility() reached with visibility: true
D/ccl_VideoCastControlle( 2765): onRemoteMediaPlayerStatusUpdated(), status: 2
D/ccl_VideoCastNotificat( 2765): onRemoteMediaPlayerMetadataUpdated() reached with status: 2
D/ccl_MyVideoCastManager( 2765): RemoteMediaPlayer::onStatusUpdated() is reached
D/ccl_MyVideoCastManager( 2765): onRemoteMediaPlayerStatusUpdated() reached 4 0
I/System.out( 2765): status: buffering
D/ccl_MyVideoCastManager( 2765): updateMiniControllersVisibility() reached with visibility: true
D/ccl_VideoCastControlle( 2765): onRemoteMediaPlayerStatusUpdated(), status: 4
D/ccl_VideoCastNotificat( 2765): onRemoteMediaPlayerMetadataUpdated() reached with status: 4
D/ccl_MyVideoCastManager( 2765): RemoteMediaPlayer::onStatusUpdated() is reached
D/ccl_MyVideoCastManager( 2765): onRemoteMediaPlayerStatusUpdated() reached 2 0
I/System.out( 2765): status: playing
D/ccl_MyVideoCastManager( 2765): updateMiniControllersVisibility() reached with visibility: true
D/ccl_VideoCastControlle( 2765): onRemoteMediaPlayerStatusUpdated(), status: 2
D/ccl_VideoCastNotificat( 2765): onRemoteMediaPlayerMetadataUpdated() reached with status: 2
D/ccl_MyVideoCastManager( 2765): RemoteMediaPlayer::onStatusUpdated() is reached
D/ccl_MyVideoCastManager( 2765): onRemoteMediaPlayerStatusUpdated() reached 4 0
I/System.out( 2765): status: buffering
D/ccl_MyVideoCastManager( 2765): updateMiniControllersVisibility() reached with visibility: true
D/ccl_VideoCastControlle( 2765): onRemoteMediaPlayerStatusUpdated(), status: 4
D/ccl_VideoCastNotificat( 2765): onRemoteMediaPlayerMetadataUpdated() reached with status: 4
D/ccl_MyVideoCastManager( 2765): RemoteMediaPlayer::onStatusUpdated() is reached
D/ccl_MyVideoCastManager( 2765): onRemoteMediaPlayerStatusUpdated() reached 2 0
I/System.out( 2765): status: playing
D/ccl_MyVideoCastManager( 2765): updateMiniControllersVisibility() reached with visibility: true
D/ccl_VideoCastControlle( 2765): onRemoteMediaPlayerStatusUpdated(), status: 2
D/ccl_VideoCastNotificat( 2765): onRemoteMediaPlayerMetadataUpdated() reached with status: 2
D/ccl_MyVideoCastManager( 2765): RemoteMediaPlayer::onStatusUpdated() is reached
D/ccl_MyVideoCastManager( 2765): onRemoteMediaPlayerStatusUpdated() reached 4 0
I/System.out( 2765): status: buffering
D/ccl_MyVideoCastManager( 2765): updateMiniControllersVisibility() reached with visibility: true
D/ccl_VideoCastControlle( 2765): onRemoteMediaPlayerStatusUpdated(), status: 4
D/ccl_VideoCastNotificat( 2765): onRemoteMediaPlayerMetadataUpdated() reached with status: 4
D/dalvikvm(  753): GC_EXPLICIT freed 967K, 7% free 27480K/29484K, paused 1ms+3ms, total 85ms
D/ccl_MyVideoCastManager( 2765): RemoteMediaPlayer::onStatusUpdated() is reached
D/ccl_MyVideoCastManager( 2765): onRemoteMediaPlayerStatusUpdated() reached 2 0

if you want to pursue then go to git and get an android sender example that uses the CCL like 'VideoCast' and debug it. You will see the important parts of the stack around status changes and Notification of such.

Upvotes: 0

Leon Nicholls
Leon Nicholls

Reputation: 4656

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.

Upvotes: 3

Related Questions