Example #1
0
        public void UpdatePlaybackState(string error)
        {
            LogHelper.Debug(Tag, "updatePlaybackState, playback state=" + Playback.State);
            long position = PlaybackStateCompat.PlaybackPositionUnknown;

            if (Playback != null && Playback.IsConnected)
            {
                position = Playback.CurrentStreamPosition;
            }

            //noinspection ResourceType
            PlaybackStateCompat.Builder stateBuilder = new PlaybackStateCompat.Builder()
                                                       .SetActions(GetAvailableActions());

            //setCustomAction(stateBuilder);
            int state = Playback.State;

            // If there is an error message, send it to the playback state:
            if (error != null)
            {
                // Error states are really only supposed to be used for errors that cause playback to
                // stop unexpectedly and persist until the user takes action to fix it.
                stateBuilder.SetErrorMessage(error);
                state = PlaybackStateCompat.StateError;
            }
            //noinspection ResourceType
            stateBuilder.SetState(state, position, 1.0f, SystemClock.ElapsedRealtime());

            // Set the activeQueueItemId if the current index is valid.
            MediaSessionCompat.QueueItem currentMusic = queueManager.CurrentMusic;
            if (currentMusic != null)
            {
                stateBuilder.SetActiveQueueItemId(currentMusic.QueueId);
            }

            serviceCallback.OnPlaybackStateUpdated(stateBuilder.Build());

            if (state == PlaybackStateCompat.StatePlaying ||
                state == PlaybackStateCompat.StatePaused)
            {
                serviceCallback.OnNotificationRequired();
            }
        }