예제 #1
0
		public static List<MediaSession.QueueItem> GetPlayingQueueFromSearch(String query,
			MusicProvider musicProvider) {

			LogHelper.Debug(Tag, "Creating playing queue for musics from search ", query);

			return ConvertToQueue(musicProvider.SearchMusic(query), MediaIDHelper.MediaIdMusicsBySearch, query);
		}
예제 #2
0
        public static List <MediaSession.QueueItem> GetPlayingQueueFromSearch(String query,
                                                                              MusicProvider musicProvider)
        {
            LogHelper.Debug(Tag, "Creating playing queue for musics from search ", query);

            return(ConvertToQueue(musicProvider.SearchMusic(query), MediaIDHelper.MediaIdMusicsBySearch, query));
        }
예제 #3
0
		public static List<MediaSession.QueueItem> GetPlayingQueue(string mediaId,
			MusicProvider musicProvider) {

			// extract the browsing hierarchy from the media ID:
			var hierarchy = MediaIDHelper.GetHierarchy(mediaId);

			if (hierarchy.Length != 2) {
				LogHelper.Error(Tag, "Could not build a playing queue for this mediaId: ", mediaId);
				return null;
			}

			var categoryType = hierarchy[0];
			var categoryValue = hierarchy[1];
			LogHelper.Debug(Tag, "Creating playing queue for ", categoryType, ",  ", categoryValue);

			IEnumerable<MediaMetadata> tracks = null;
			// This sample only supports genre and by_search category types.
			if (categoryType == MediaIDHelper.MediaIdMusicsByGenre) {
				tracks = musicProvider.GetMusicsByGenre(categoryValue);
			} else if (categoryType == MediaIDHelper.MediaIdMusicsBySearch) {
				tracks = musicProvider.SearchMusic(categoryValue);
			}

			if (tracks == null) {
				LogHelper.Error(Tag, "Unrecognized category type: ", categoryType, " for mediaId ", mediaId);
				return null;
			}

			return ConvertToQueue(tracks, hierarchy[0], hierarchy[1]);
		}
예제 #4
0
        public static List <MediaSession.QueueItem> GetRandomQueue(MusicProvider musicProvider)
        {
            var genres = musicProvider.Genres;

            if (genres.Count <= 1)
            {
                return(new List <MediaSession.QueueItem> ());
            }
            var genre  = genres [1];
            var tracks = musicProvider.GetMusicsByGenre(genre);

            return(ConvertToQueue(tracks, MediaIDHelper.MediaIdMusicsByGenre, genre));
        }
        public static List <MediaSession.QueueItem> GetRandomQueue(MusicProvider musicProvider)
        {
            List <string> genres = musicProvider.Genres;

            if (genres.Count <= 1)
            {
                return(new List <MediaSession.QueueItem> ());
            }

            string genre = genres [1];
            IEnumerable <MediaMetadata> tracks = musicProvider.GetMusicsByGenre(genre);

            return(ConvertToQueue(tracks, MediaIDHelper.MediaIdMusicsByGenre, genre));
        }
예제 #6
0
		public Playback(MusicService service, MusicProvider musicProvider) {
			this.service = service;
			this.musicProvider = musicProvider;
			audioManager = (AudioManager) service.GetSystemService(Context.AudioService);
			wifiLock = ((WifiManager) service.GetSystemService(Context.WifiService))
				.CreateWifiLock(WifiMode.Full, "sample_lock");
			mAudioNoisyReceiver.OnReceiveImpl = (context, intent) => {
				if (AudioManager.ActionAudioBecomingNoisy == intent.Action) {
					LogHelper.Debug(Tag, "Headphones disconnected.");
					if (IsPlaying) {
						var i = new Intent(context, typeof(MusicService));
						i.SetAction(MusicService.ActionCmd);
						i.PutExtra(MusicService.CmdName, MusicService.CmdPause);
						service.StartService(i);
					}
				}
			};
		}
예제 #7
0
 public Playback(MusicService service, MusicProvider musicProvider)
 {
     this.service       = service;
     this.musicProvider = musicProvider;
     audioManager       = (AudioManager)service.GetSystemService(Context.AudioService);
     wifiLock           = ((WifiManager)service.GetSystemService(Context.WifiService))
                          .CreateWifiLock(WifiMode.Full, "sample_lock");
     mAudioNoisyReceiver.OnReceiveImpl = (context, intent) => {
         if (AudioManager.ActionAudioBecomingNoisy == intent.Action)
         {
             LogHelper.Debug(Tag, "Headphones disconnected.");
             if (IsPlaying)
             {
                 var i = new Intent(context, typeof(MusicService));
                 i.SetAction(MusicService.ActionCmd);
                 i.PutExtra(MusicService.CmdName, MusicService.CmdPause);
                 service.StartService(i);
             }
         }
     };
 }
예제 #8
0
        public static List <MediaSession.QueueItem> GetPlayingQueue(string mediaId,
                                                                    MusicProvider musicProvider)
        {
            // extract the browsing hierarchy from the media ID:
            var hierarchy = MediaIDHelper.GetHierarchy(mediaId);

            if (hierarchy.Length != 2)
            {
                LogHelper.Error(Tag, "Could not build a playing queue for this mediaId: ", mediaId);
                return(null);
            }

            var categoryType  = hierarchy[0];
            var categoryValue = hierarchy[1];

            LogHelper.Debug(Tag, "Creating playing queue for ", categoryType, ",  ", categoryValue);

            IEnumerable <MediaMetadata> tracks = null;

            // This sample only supports genre and by_search category types.
            if (categoryType == MediaIDHelper.MediaIdMusicsByGenre)
            {
                tracks = musicProvider.GetMusicsByGenre(categoryValue);
            }
            else if (categoryType == MediaIDHelper.MediaIdMusicsBySearch)
            {
                tracks = musicProvider.SearchMusic(categoryValue);
            }

            if (tracks == null)
            {
                LogHelper.Error(Tag, "Unrecognized category type: ", categoryType, " for mediaId ", mediaId);
                return(null);
            }

            return(ConvertToQueue(tracks, hierarchy[0], hierarchy[1]));
        }
예제 #9
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);
        }
예제 #10
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);
		}
예제 #11
0
		public static List<MediaSession.QueueItem> GetRandomQueue(MusicProvider musicProvider) {
			var genres = musicProvider.Genres;
			if (genres.Count <= 1)
				return new List<MediaSession.QueueItem> ();
			var genre = genres [1];
			var tracks = musicProvider.GetMusicsByGenre (genre);

			return ConvertToQueue (tracks, MediaIDHelper.MediaIdMusicsByGenre, genre);
		}