public List <MediaSession.QueueItem> GetPlayingQueue(string mediaId)
        {
            string[] hierarchy = HierarchyHelper.GetHierarchy(mediaId);

            if (hierarchy.Length != 2)
            {
                return(null);
            }

            string categoryType  = hierarchy[0];
            string categoryValue = hierarchy[1];

            IEnumerable <MediaMetadata> tracks = null;

            if (categoryType == HierarchyHelper.PodcastsByMonth)
            {
                tracks = this.GetMusicsByMonth(categoryValue);
            }
            else if (categoryType == HierarchyHelper.PodcastsBySearch)
            {
                tracks = this.SearchMusic(categoryValue);
            }

            if (tracks == null)
            {
                return(null);
            }

            return(ConvertToQueue(tracks, hierarchy[0], hierarchy[1]));
        }
        private void LoadChildrenImpl(string parentId, Result result)
        {
            var mediaItems = new JavaList <MediaBrowser.MediaItem>();

            if (HierarchyHelper.PodcastRoot == parentId)
            {
                Logger.Debug("Load ROOT");
                mediaItems.Add(new MediaBrowser.MediaItem(
                                   new MediaDescription.Builder()
                                   .SetMediaId(HierarchyHelper.PodcastsByMonth)
                                   .SetTitle("All Playlists")
                                   .SetIconUri(Android.Net.Uri.Parse("android.resource://com.henry.mypodcast/drawable/ic_by_genre"))
                                   .SetSubtitle("Bursts By Playlist")
                                   .Build(), MediaItemFlags.Browsable));
            }
            else if (HierarchyHelper.PodcastsByMonth == parentId)
            {
                Logger.Debug("Load BYMONTH List");
                foreach (var playlist in _musicProvider._playlists)
                {
                    var item = new MediaBrowser.MediaItem(
                        new MediaDescription.Builder()
                        .SetMediaId(HierarchyHelper.PodcastsByMonth + HierarchyHelper.CategorySeparator + playlist.Id.ToString())
                        .SetTitle(playlist.Name)
                        .SetSubtitle($"{playlist.Name} Bursts")
                        .Build(), MediaItemFlags.Browsable);
                    mediaItems.Add(item);
                }
            }
            else if (parentId.StartsWith(HierarchyHelper.PodcastsByMonth))
            {
                var playlistId = int.Parse(HierarchyHelper.GetHierarchy(parentId)[1]);
                Logger.Debug("Load List of Podcasts for Month");
                foreach (var track in _musicProvider.GetMusicsByPlaylist(playlistId))
                {
                    var hierarchyAwareMediaID = HierarchyHelper.EncodeMediaID(
                        track.Description.MediaId, HierarchyHelper.PodcastsByMonth, playlistId.ToString());
                    var trackCopy = new MediaMetadata.Builder(track)
                                    .PutString(MediaMetadata.MetadataKeyMediaId, hierarchyAwareMediaID)
                                    .Build();
                    var bItem = new MediaBrowser.MediaItem(
                        trackCopy.Description, MediaItemFlags.Playable);
                    mediaItems.Add(bItem);
                }
            }
            result.SendResult(mediaItems);
        }
        private MediaSessionCallback CreateMediaSessionCallback()
        {
            var mediaCallback = new MediaSessionCallback();

            mediaCallback.OnPlayImpl = () => {
                Logger.Debug("OnPlayImpl");

                if (_playingQueue == null || _playingQueue.Count == 0)
                {
                    _playingQueue = new List <MediaSession.QueueItem>(_musicProvider.GetRandomQueue());
                    _session.SetQueue(_playingQueue);
                    _session.SetQueueTitle("Random music");
                    _currentIndexQueue = 0;
                }

                if (_playingQueue != null && _playingQueue.Count != 0)
                {
                    HandlePlayRequest();
                }
            };
            mediaCallback.OnSkipToQueueItemImpl = (id) => {
                Logger.Debug("OnSkipToQueueItem");

                if (_playingQueue != null && _playingQueue.Count != 0)
                {
                    _currentIndexQueue = -1;
                    int index = 0;
                    foreach (var item in _playingQueue)
                    {
                        if (id == item.QueueId)
                        {
                            _currentIndexQueue = index;
                        }
                        index++;
                    }
                    HandlePlayRequest();
                }
            };
            mediaCallback.OnSeekToImpl = (pos) => {
                Logger.Debug("OnSeekToImpl:");
                _musicPlayer.SeekTo((int)pos);
            };
            mediaCallback.OnPlayFromMediaIdImpl = (mediaId, extras) => {
                Logger.Debug($"OnPlayFromMediaIdImpl mediaId: {mediaId}");

                _playingQueue = _musicProvider.GetPlayingQueue(mediaId);
                _session.SetQueue(_playingQueue);

                string[] hierarchies = HierarchyHelper.GetHierarchy(mediaId);
                string   month       = hierarchies != null && hierarchies.Length == 2 ? hierarchies[1] : string.Empty;
                var      queueTitle  = $"{month} Podcasts";

                _session.SetQueueTitle(queueTitle);

                if (_playingQueue != null && _playingQueue.Count != 0)
                {
                    _currentIndexQueue = -1;
                    int index = 0;
                    foreach (var item in _playingQueue)
                    {
                        if (mediaId == item.Description.MediaId)
                        {
                            _currentIndexQueue = index;
                        }
                        index++;
                    }

                    if (_currentIndexQueue < 0)
                    {
                        Logger.Error($"OnPlayFromMediaIdImpl: media ID {mediaId} not be found.");
                    }
                    else
                    {
                        HandlePlayRequest();
                    }
                }
            };
            mediaCallback.OnPauseImpl = () => {
                OnPause();
            };
            mediaCallback.OnStopImpl = () => {
                OnStop(null);
            };
            mediaCallback.OnSkipToNextImpl = () => {
                Logger.Debug("OnSkipToNextImpl");
                _currentIndexQueue++;
                if (_playingQueue != null && _currentIndexQueue >= _playingQueue.Count)
                {
                    _currentIndexQueue = 0;
                }
                if (this.isIndexPlayable(_currentIndexQueue, _playingQueue))
                {
                    HandlePlayRequest();
                }
                else
                {
                    OnStop("Cannot skip");
                }
            };
            mediaCallback.OnSkipToPreviousImpl = () => {
                Logger.Debug("OnSkipToPreviousImpl");
                _currentIndexQueue--;
                if (_playingQueue != null && _currentIndexQueue < 0)
                {
                    _currentIndexQueue = 0;
                }
                if (this.isIndexPlayable(_currentIndexQueue, _playingQueue))
                {
                    HandlePlayRequest();
                }
                else
                {
                    OnStop("Cannot skip");
                }
            };
            mediaCallback.OnCustomActionImpl = (action, extras) => {
                if (CustomActionFavorite == action)
                {
                    Logger.Info("OnCustomActionImpl");
                    var track = GetCurrentPlayingMusic();
                    if (track != null)
                    {
                        var musicId = track.GetString(MediaMetadata.MetadataKeyMediaId);
                        _musicProvider.SetFavorite(musicId, !_musicProvider.IsFavorite(musicId));
                    }
                    UpdatePlaybackState(null);
                }
                else
                {
                    Logger.Error($"Unsupported action: {action}");
                }
            };
            mediaCallback.OnPlayFromSearchImpl = (query, extras) => {
                Logger.Debug($"OnPlayFromSearchImpl  query={query}");

                if (string.IsNullOrEmpty(query))
                {
                    _playingQueue = new List <MediaSession.QueueItem>(_musicProvider.GetRandomQueue());
                }
                else
                {
                    _playingQueue = new List <MediaSession.QueueItem>(_musicProvider.GetPlayingQueueFromSearch(query));
                }

                _session.SetQueue(_playingQueue);

                if (_playingQueue != null && _playingQueue.Count != 0)
                {
                    _currentIndexQueue = 0;

                    HandlePlayRequest();
                }
                else
                {
                    OnStop("0 Found.");
                }
            };
            return(mediaCallback);
        }