コード例 #1
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);
        }
コード例 #2
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 ();

			Context 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);
		}