Esempio n. 1
0
 public void HandlePlayRequest()
 {
     LogHelper.Debug(Tag, "handlePlayRequest: mState=" + Playback.State);
     MediaSessionCompat.QueueItem currentMusic = queueManager.CurrentMusic;
     if (currentMusic != null)
     {
         serviceCallback.OnPlaybackStart();
         Playback.Play(currentMusic);
     }
 }
Esempio n. 2
0
        public void SwitchToPlayback(IPlayback playback, bool resumePlaying)
        {
            if (playback == null)
            {
                throw new ArgumentException("Playback cannot be null");
            }
            // suspend the current one.
            int oldState       = Playback.State;
            int pos            = Playback.CurrentStreamPosition;
            var currentMediaId = Playback.CurrentMediaId;

            Playback.Stop(false);
            Playback.Callback = this;
            Playback.CurrentStreamPosition = pos < 0 ? 0 : pos;
            Playback.CurrentMediaId        = currentMediaId;
            Playback.Start();
            // finally swap the instance
            Playback = playback;
            switch (oldState)
            {
            case PlaybackStateCompat.StateBuffering:
            case PlaybackStateCompat.StateConnecting:
            case PlaybackStateCompat.StatePaused:
                Playback.Pause();
                break;

            case PlaybackStateCompat.StatePlaying:
                MediaSessionCompat.QueueItem currentMusic = queueManager.CurrentMusic;
                if (resumePlaying && currentMusic != null)
                {
                    Playback.Play(currentMusic);
                }
                else if (!resumePlaying)
                {
                    Playback.Pause();
                }
                else
                {
                    Playback.Stop(true);
                }
                break;

            case PlaybackStateCompat.StateNone:
                break;

            default:
                LogHelper.Debug(Tag, "Default called. Old state is ", oldState);
                break;
            }
        }
Esempio n. 3
0
        static List <MediaSessionCompat.QueueItem> ConvertToQueue(IEnumerable <MediaMetadataCompat> tracks, params string[] categories)
        {
            var queue = new List <MediaSessionCompat.QueueItem>();
            int count = 0;

            foreach (var track in tracks)
            {
                var trackCopy = new MediaMetadataCompat.Builder(track)
                                .PutString(MediaMetadataCompat.MetadataKeyMediaId, track.Description.MediaId)
                                .Build();

                var item = new MediaSessionCompat.QueueItem(trackCopy.Description, count++);
                queue.Add(item);
            }
            return(queue);
        }
Esempio n. 4
0
        public void UpdateMetadata()
        {
            MediaSessionCompat.QueueItem currentMusic = CurrentMusic;
            if (currentMusic == null)
            {
                listener.OnMetadataRetrieveError();
                return;
            }
            string musicId  = currentMusic.Description.MediaId;
            var    metadata = musicProvider.GetMusic(musicId);

            if (metadata == null)
            {
                throw new ArgumentException("Invalid musicId " + musicId);
            }

            listener.OnMetadataChanged(metadata);

            // Set the proper album artwork on the media session, so it can be shown in the
            // locked screen and in other places.
            if (metadata.Description.IconBitmap == null &&
                metadata.Description.IconUri != null)
            {
                var albumUri = metadata.Description.IconUri.ToString();
                AlbumArtCache.Instance.Fetch(albumUri, new AlbumArtCache.FetchListener()
                {
                    OnFetched = (artUrl, bitmap, icon) =>
                    {
                        musicProvider.UpdateMusicArt(musicId, bitmap, icon);

                        // If we are still playing the same music, notify the listeners:
                        var cm = CurrentMusic;
                        if (cm == null)
                        {
                            return;
                        }
                        var currentPlayingId = cm.Description.MediaId;
                        if (musicId == currentPlayingId)
                        {
                            listener.OnMetadataChanged(musicProvider.GetMusic(currentPlayingId));
                        }
                    }
                });
            }
        }
Esempio n. 5
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();
            }
        }