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]));
        }
Beispiel #2
0
        public void Play(MediaSession.QueueItem item)
        {
            var mediaHasChanged = InitPlayerStates(item.Description.MediaId);

            if (MusicPlayerState == PlaybackStateCode.Paused && !mediaHasChanged && _mediaPlayer != null)
            {
                ConfigMediaPlayerState();
            }
            else
            {
                MusicPlayerState = PlaybackStateCode.Stopped;
                CleanUp(false);
                MediaMetadata track = _musicProvider.GetMusic(
                    HierarchyHelper.ExtractMusicIDFromMediaID(item.Description.MediaId));

                string source = track.GetString(MusicProvider.PodcastSource);

                try
                {
                    _mediaPlayer.Reset();
                    MusicPlayerState = PlaybackStateCode.Buffering;
                    _mediaPlayer.SetAudioStreamType(Android.Media.Stream.Music);
                    _mediaPlayer.SetDataSource(source);
                    _mediaPlayer.PrepareAsync();
                    _wifiLock.Acquire();
                    _musicService.OnPlaybackStatusChanged(MusicPlayerState);
                }
                catch (Exception ex)
                {
                    Logger.Error(ex, "Error playing song");
                    _musicService.OnError(ex.Message);
                }
            }
        }
 private MediaMetadata GetCurrentPlayingMusic()
 {
     if (this.isIndexPlayable(_currentIndexQueue, _playingQueue))
     {
         var item = _playingQueue[_currentIndexQueue];
         if (item != null)
         {
             Logger.Debug("GetCurrentPlayingMusic");
             return(_musicProvider.GetMusic(
                        HierarchyHelper.ExtractMusicIDFromMediaID(item.Description.MediaId)));
         }
     }
     return(null);
 }
        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 List <MediaSession.QueueItem> ConvertToQueue(IEnumerable <MediaMetadata> tracks, params string[] categories)
        {
            var queue = new List <MediaSession.QueueItem>();
            int count = 0;

            foreach (var track in tracks)
            {
                string        hierarchyAwareMediaID = HierarchyHelper.EncodeMediaID(track.Description.MediaId, categories);
                MediaMetadata trackCopy             = new MediaMetadata.Builder(track)
                                                      .PutString(MediaMetadata.MetadataKeyMediaId, hierarchyAwareMediaID)
                                                      .Build();

                var item = new MediaSession.QueueItem(trackCopy.Description, count++);
                queue.Add(item);
            }
            return(queue);
        }
        private void UpdateMetadata()
        {
            if (!this.isIndexPlayable(_currentIndexQueue, _playingQueue))
            {
                Logger.Error("Can't retrieve current metadata.");
                UpdatePlaybackState("Error no data.");
                return;
            }
            MediaSession.QueueItem queueItem = _playingQueue[_currentIndexQueue];
            string        musicId            = HierarchyHelper.ExtractMusicIDFromMediaID(queueItem.Description.MediaId);
            MediaMetadata track   = _musicProvider.GetMusic(musicId);
            string        trackId = track.GetString(MediaMetadata.MetadataKeyMediaId);

            if (musicId != trackId)
            {
                var e = new InvalidOperationException("track ID should match musicId.");
                throw e;
            }
            Logger.Debug($"Updating metadata for MusicID= {musicId}");
            _session.SetMetadata(track);
        }
        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);
        }