Beispiel #1
0
        void HandlePlayRequest()
        {
            LogHelper.Debug(Tag, "handlePlayRequest: mState=" + playback.State);

            delayedStopHandler.RemoveCallbacksAndMessages(null);
            if (!serviceStarted)
            {
                LogHelper.Verbose(Tag, "Starting service");
                // The MusicService needs to keep running even after the calling MediaBrowser
                // is disconnected. Call startService(Intent) and then stopSelf(..) when we no longer
                // need to play media.
                StartService(new Intent(ApplicationContext, typeof(MusicService)));
                serviceStarted = true;
            }

            if (!session.Active)
            {
                session.Active = true;
            }

            if (QueueHelper.isIndexPlayable(currentIndexOnQueue, playingQueue))
            {
                UpdateMetadata();
                playback.Play(playingQueue [currentIndexOnQueue]);
            }
        }
Beispiel #2
0
        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);
                        }
                    }
                });
            }
        }
Beispiel #3
0
 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);
 }
Beispiel #4
0
        void UpdatePlaybackState(String error)
        {
            LogHelper.Debug(Tag, "updatePlaybackState, playback state=" + playback.State);
            var position = PlaybackState.PlaybackPositionUnknown;

            if (playback != null && playback.IsConnected)
            {
                position = playback.CurrentStreamPosition;
            }

            var stateBuilder = new PlaybackState.Builder()
                               .SetActions(GetAvailableActions());

            SetCustomAction(stateBuilder);
            var 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 = PlaybackStateCode.Error;
            }
            stateBuilder.SetState(state, position, 1.0f, SystemClock.ElapsedRealtime());

            // Set the activeQueueItemId if the current index is valid.
            if (QueueHelper.isIndexPlayable(currentIndexOnQueue, playingQueue))
            {
                var item = playingQueue [currentIndexOnQueue];
                stateBuilder.SetActiveQueueItemId(item.QueueId);
            }

            session.SetPlaybackState(stateBuilder.Build());

            if (state == PlaybackStateCode.Playing || state == PlaybackStateCode.Paused)
            {
                mediaNotificationManager.StartNotification();
            }
        }
Beispiel #5
0
        public override void OnCreate()
        {
            base.OnCreate();
            LogHelper.Debug(Tag, "onCreate");

            playingQueue     = new List <MediaSession.QueueItem> ();
            musicProvider    = new MusicProvider();
            packageValidator = new PackageValidator(this);

            session      = new MediaSession(this, "MusicService");
            SessionToken = session.SessionToken;
            var mediaCallback = new MediaSessionCallback();

            mediaCallback.OnPlayImpl = () => {
                LogHelper.Debug(Tag, "play");

                if (playingQueue == null || playingQueue.Count != 0)
                {
                    playingQueue = new List <MediaSession.QueueItem> (QueueHelper.GetRandomQueue(musicProvider));
                    session.SetQueue(playingQueue);
                    session.SetQueueTitle(GetString(Resource.String.random_queue_title));
                    currentIndexOnQueue = 0;
                }

                if (playingQueue != null && playingQueue.Count != 0)
                {
                    HandlePlayRequest();
                }
            };
            mediaCallback.OnSkipToQueueItemImpl = (id) => {
                LogHelper.Debug(Tag, "OnSkipToQueueItem:" + id);

                if (playingQueue != null && playingQueue.Count != 0)
                {
                    currentIndexOnQueue = QueueHelper.GetMusicIndexOnQueue(playingQueue, id);
                    HandlePlayRequest();
                }
            };
            mediaCallback.OnSeekToImpl = (pos) => {
                LogHelper.Debug(Tag, "onSeekTo:", pos);
                playback.SeekTo((int)pos);
            };
            mediaCallback.OnPlayFromMediaIdImpl = (mediaId, extras) => {
                LogHelper.Debug(Tag, "playFromMediaId mediaId:", mediaId, "  extras=", extras);

                playingQueue = QueueHelper.GetPlayingQueue(mediaId, musicProvider);
                session.SetQueue(playingQueue);
                var queueTitle = GetString(Resource.String.browse_musics_by_genre_subtitle,
                                           MediaIDHelper.ExtractBrowseCategoryValueFromMediaID(mediaId));
                session.SetQueueTitle(queueTitle);

                if (playingQueue != null && playingQueue.Count != 0)
                {
                    currentIndexOnQueue = QueueHelper.GetMusicIndexOnQueue(playingQueue, mediaId);

                    if (currentIndexOnQueue < 0)
                    {
                        LogHelper.Error(Tag, "playFromMediaId: media ID ", mediaId,
                                        " could not be found on queue. Ignoring.");
                    }
                    else
                    {
                        HandlePlayRequest();
                    }
                }
            };
            mediaCallback.OnPauseImpl = () => {
                LogHelper.Debug(Tag, "pause. current state=" + playback.State);
                HandlePauseRequest();
            };
            mediaCallback.OnStopImpl = () => {
                LogHelper.Debug(Tag, "stop. current state=" + playback.State);
                HandleStopRequest(null);
            };
            mediaCallback.OnSkipToNextImpl = () => {
                LogHelper.Debug(Tag, "skipToNext");
                currentIndexOnQueue++;
                if (playingQueue != null && currentIndexOnQueue >= playingQueue.Count)
                {
                    currentIndexOnQueue = 0;
                }
                if (QueueHelper.isIndexPlayable(currentIndexOnQueue, playingQueue))
                {
                    HandlePlayRequest();
                }
                else
                {
                    LogHelper.Error(Tag, "skipToNext: cannot skip to next. next Index=" +
                                    currentIndexOnQueue + " queue length=" +
                                    (playingQueue == null ? "null" : playingQueue.Count.ToString()));
                    HandleStopRequest("Cannot skip");
                }
            };
            mediaCallback.OnSkipToPreviousImpl = () => {
                LogHelper.Debug(Tag, "skipToPrevious");
                currentIndexOnQueue--;
                if (playingQueue != null && currentIndexOnQueue < 0)
                {
                    currentIndexOnQueue = 0;
                }
                if (QueueHelper.isIndexPlayable(currentIndexOnQueue, playingQueue))
                {
                    HandlePlayRequest();
                }
                else
                {
                    LogHelper.Error(Tag, "skipToPrevious: cannot skip to previous. previous Index=" +
                                    currentIndexOnQueue + " queue length=" +
                                    (playingQueue == null ? "null" : playingQueue.Count.ToString()));
                    HandleStopRequest("Cannot skip");
                }
            };
            mediaCallback.OnCustomActionImpl = (action, extras) => {
                if (CustomActionThumbsUp == action)
                {
                    LogHelper.Info(Tag, "onCustomAction: favorite for current track");
                    var track = GetCurrentPlayingMusic();
                    if (track != null)
                    {
                        var musicId = track.GetString(MediaMetadata.MetadataKeyMediaId);
                        musicProvider.SetFavorite(musicId, !musicProvider.IsFavorite(musicId));
                    }
                    UpdatePlaybackState(null);
                }
                else
                {
                    LogHelper.Error(Tag, "Unsupported action: ", action);
                }
            };
            mediaCallback.OnPlayFromSearchImpl = (query, extras) => {
                LogHelper.Debug(Tag, "playFromSearch  query=", query);

                if (string.IsNullOrEmpty(query))
                {
                    playingQueue = new List <MediaSession.QueueItem> (QueueHelper.GetRandomQueue(musicProvider));
                }
                else
                {
                    playingQueue = new List <MediaSession.QueueItem> (QueueHelper.GetPlayingQueueFromSearch(query, musicProvider));
                }

                LogHelper.Debug(Tag, "playFromSearch  playqueue.length=" + playingQueue.Count);
                session.SetQueue(playingQueue);

                if (playingQueue != null && playingQueue.Count != 0)
                {
                    currentIndexOnQueue = 0;

                    HandlePlayRequest();
                }
                else
                {
                    HandleStopRequest(GetString(Resource.String.no_search_results));
                }
            };
            session.SetCallback(mediaCallback);
            session.SetFlags(MediaSessionFlags.HandlesMediaButtons |
                             MediaSessionFlags.HandlesTransportControls);

            playback          = new Playback(this, musicProvider);
            playback.State    = PlaybackStateCode.None;
            playback.Callback = this;
            playback.Start();

            var context = ApplicationContext;
            var intent  = new Intent(context, typeof(MusicPlayerActivity));
            var pi      = PendingIntent.GetActivity(context, 99 /*request code*/,
                                                    intent, PendingIntentFlags.UpdateCurrent);

            session.SetSessionActivity(pi);

            var extraBundle = new Bundle();

            CarHelper.SetSlotReservationFlags(extraBundle, true, true, true);
            session.SetExtras(extraBundle);

            UpdatePlaybackState(null);

            mediaNotificationManager = new MediaNotificationManager(this);
        }