Example #1
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 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);
        }