void UpdateMetadata() { if (!QueueHelper.isIndexPlayable(currentIndexOnQueue, playingQueue)) { LogHelper.Error(Tag, "Can't retrieve current metadata."); UpdatePlaybackState(Resources.GetString(Resource.String.error_no_metadata)); return; } var queueItem = playingQueue [currentIndexOnQueue]; var musicId = MediaIDHelper.ExtractMusicIDFromMediaID( queueItem.Description.MediaId); var track = musicProvider.GetMusic(musicId); var trackId = track.GetString(MediaMetadata.MetadataKeyMediaId); if (musicId != trackId) { var e = new InvalidOperationException("track ID should match musicId."); LogHelper.Error(Tag, "track ID should match musicId.", " musicId=", musicId, " trackId=", trackId, " mediaId from queueItem=", queueItem.Description.MediaId, " title from queueItem=", queueItem.Description.Title, " mediaId from track=", track.Description.MediaId, " title from track=", track.Description.Title, " source.hashcode from track=", track.GetString( MusicProvider.CustomMetadataTrackSource).GetHashCode(), e); throw e; } LogHelper.Debug(Tag, "Updating metadata for MusicID= " + musicId); session.SetMetadata(track); // Set the proper album artwork on the media session, so it can be shown in the // locked screen and in other places. if (track.Description.IconBitmap == null && track.Description.IconUri != null) { var albumUri = track.Description.IconUri.ToString(); AlbumArtCache.Instance.Fetch(albumUri, new AlbumArtCache.FetchListener { OnFetched = (artUrl, bitmap, icon) => { var qItem = playingQueue [currentIndexOnQueue]; var trackItem = musicProvider.GetMusic(trackId); trackItem = new MediaMetadata.Builder(trackItem) .PutBitmap(MediaMetadata.MetadataKeyAlbumArt, bitmap) .PutBitmap(MediaMetadata.MetadataKeyDisplayIcon, icon) .Build(); musicProvider.UpdateMusic(trackId, trackItem); // If we are still playing the same music var currentPlayingId = MediaIDHelper.ExtractMusicIDFromMediaID( qItem.Description.MediaId); if (trackId == currentPlayingId) { session.SetMetadata(trackItem); } } }); } }
public void Play(MediaSession.QueueItem item) { playOnFocusGain = true; TryToGetAudioFocus(); RegisterAudioNoisyReceiver(); string mediaId = item.Description.MediaId; bool mediaHasChanged = mediaId != currentMediaId; if (mediaHasChanged) { currentPosition = 0; currentMediaId = mediaId; } if (State == PlaybackStateCode.Paused && !mediaHasChanged && mediaPlayer != null) { ConfigMediaPlayerState(); } else { State = PlaybackStateCode.Stopped; RelaxResources(false); MediaMetadata track = musicProvider.GetMusic( MediaIDHelper.ExtractMusicIDFromMediaID(item.Description.MediaId)); string source = track.GetString(MusicProvider.CustomMetadataTrackSource); try { CreateMediaPlayerIfNeeded(); State = PlaybackStateCode.Buffering; mediaPlayer.SetAudioStreamType(Android.Media.Stream.Music); mediaPlayer.SetDataSource(source); mediaPlayer.PrepareAsync(); wifiLock.Acquire(); if (Callback != null) { Callback.OnPlaybackStatusChanged(State); } } catch (IOException ex) { LogHelper.Error(Tag, ex, "Exception playing song"); if (Callback != null) { Callback.OnError(ex.Message); } } } }
MediaMetadata GetCurrentPlayingMusic() { if (QueueHelper.isIndexPlayable(currentIndexOnQueue, playingQueue)) { var item = playingQueue [currentIndexOnQueue]; if (item != null) { LogHelper.Debug(Tag, "getCurrentPlayingMusic for musicId=", item.Description.MediaId); return(musicProvider.GetMusic( MediaIDHelper.ExtractMusicIDFromMediaID(item.Description.MediaId))); } } return(null); }