Search in sources :

Example 1 with MediaStatus

use of com.google.android.gms.cast.MediaStatus in project AntennaPod by AntennaPod.

the class RemotePSMP method onRemoteMediaPlayerStatusUpdated.

private void onRemoteMediaPlayerStatusUpdated() {
    MediaStatus status = castMgr.getMediaStatus();
    if (status == null) {
        Log.d(TAG, "Received null MediaStatus");
        return;
    } else {
        Log.d(TAG, "Received remote status/media update. New state=" + status.getPlayerState());
    }
    int state = status.getPlayerState();
    int oldState = remoteState;
    remoteMedia = status.getMediaInfo();
    boolean mediaChanged = !CastUtils.matches(remoteMedia, media);
    boolean stateChanged = state != oldState;
    if (!mediaChanged && !stateChanged) {
        Log.d(TAG, "Both media and state haven't changed, so nothing to do");
        return;
    }
    Playable currentMedia = mediaChanged ? localVersion(remoteMedia) : media;
    Playable oldMedia = media;
    int position = (int) status.getStreamPosition();
    // check for incompatible states
    if ((state == MediaStatus.PLAYER_STATE_PLAYING || state == MediaStatus.PLAYER_STATE_PAUSED) && currentMedia == null) {
        Log.w(TAG, "RemoteMediaPlayer returned playing or pausing state, but with no media");
        state = MediaStatus.PLAYER_STATE_UNKNOWN;
        stateChanged = oldState != MediaStatus.PLAYER_STATE_UNKNOWN;
    }
    if (stateChanged) {
        remoteState = state;
    }
    if (mediaChanged && stateChanged && oldState == MediaStatus.PLAYER_STATE_PLAYING && state != MediaStatus.PLAYER_STATE_IDLE) {
        callback.onPlaybackPause(null, INVALID_TIME);
        // We don't want setPlayerStatus to handle the onPlaybackPause callback
        setPlayerStatus(PlayerStatus.INDETERMINATE, currentMedia);
    }
    setBuffering(state == MediaStatus.PLAYER_STATE_BUFFERING);
    switch(state) {
        case MediaStatus.PLAYER_STATE_PLAYING:
            if (!stateChanged) {
                //These steps are necessary because they won't be performed by setPlayerStatus()
                if (position >= 0) {
                    currentMedia.setPosition(position);
                }
                currentMedia.onPlaybackStart();
            }
            setPlayerStatus(PlayerStatus.PLAYING, currentMedia, position);
            break;
        case MediaStatus.PLAYER_STATE_PAUSED:
            setPlayerStatus(PlayerStatus.PAUSED, currentMedia, position);
            break;
        case MediaStatus.PLAYER_STATE_BUFFERING:
            setPlayerStatus((mediaChanged || playerStatus == PlayerStatus.PREPARING) ? PlayerStatus.PREPARING : PlayerStatus.SEEKING, currentMedia, currentMedia != null ? currentMedia.getPosition() : INVALID_TIME);
            break;
        case MediaStatus.PLAYER_STATE_IDLE:
            int reason = status.getIdleReason();
            switch(reason) {
                case MediaStatus.IDLE_REASON_CANCELED:
                    // Essentially means stopped at the request of a user
                    callback.onPlaybackEnded(null, true);
                    setPlayerStatus(PlayerStatus.STOPPED, currentMedia);
                    if (oldMedia != null) {
                        if (position >= 0) {
                            oldMedia.setPosition(position);
                        }
                        callback.onPostPlayback(oldMedia, false, false, false);
                    }
                    // onPlaybackEnded pretty much takes care of updating the UI
                    return;
                case MediaStatus.IDLE_REASON_INTERRUPTED:
                    // Not sure if currentMedia already reflects the to be loaded one
                    if (mediaChanged && oldState == MediaStatus.PLAYER_STATE_PLAYING) {
                        callback.onPlaybackPause(null, INVALID_TIME);
                        setPlayerStatus(PlayerStatus.INDETERMINATE, currentMedia);
                    }
                    setPlayerStatus(PlayerStatus.PREPARING, currentMedia);
                    break;
                case MediaStatus.IDLE_REASON_NONE:
                    // This probably only happens when we connected but no command has been sent yet.
                    setPlayerStatus(PlayerStatus.INITIALIZED, currentMedia);
                    break;
                case MediaStatus.IDLE_REASON_FINISHED:
                    // This is our onCompletionListener...
                    if (mediaChanged && currentMedia != null) {
                        media = currentMedia;
                    }
                    endPlayback(true, false, true, true);
                    return;
                case MediaStatus.IDLE_REASON_ERROR:
                    Log.w(TAG, "Got an error status from the Chromecast. Skipping, if possible, to the next episode...");
                    callback.onMediaPlayerInfo(CAST_ERROR_PRIORITY_HIGH, R.string.cast_failed_media_error_skipping);
                    endPlayback(false, false, true, true);
                    return;
            }
            break;
        case MediaStatus.PLAYER_STATE_UNKNOWN:
            if (playerStatus != PlayerStatus.INDETERMINATE || media != currentMedia) {
                setPlayerStatus(PlayerStatus.INDETERMINATE, currentMedia);
            }
            break;
        default:
            Log.wtf(TAG, "Remote media state undetermined!");
    }
    if (mediaChanged) {
        callback.onMediaChanged(true);
        if (oldMedia != null) {
            callback.onPostPlayback(oldMedia, false, false, currentMedia != null);
        }
    }
}
Also used : Playable(de.danoeh.antennapod.core.util.playback.Playable) MediaStatus(com.google.android.gms.cast.MediaStatus)

Aggregations

MediaStatus (com.google.android.gms.cast.MediaStatus)1 Playable (de.danoeh.antennapod.core.util.playback.Playable)1