예제 #1
0
    private void UpdatePlaybackState(PlaybackStateCode state)
    {
        if (mediaSession == null || mediaPlayer == null)
        {
            return;
        }

        try
        {
            PlaybackState.Builder stateBuilder = new PlaybackState.Builder()
                                                 .SetActions(
                PlaybackState.ActionPause |
                PlaybackState.ActionPlay |
                PlaybackState.ActionPlayPause |
                PlaybackState.ActionSkipToNext |
                PlaybackState.ActionSkipToPrevious |
                PlaybackState.ActionStop
                )
                                                 .SetState(state, Position, 1.0f, SystemClock.ElapsedRealtime());

            mediaSession.SetPlaybackState(stateBuilder.Build());

            OnStatusChanged(EventArgs.Empty);

            if (state == PlaybackStateCode.Playing || state == PlaybackStateCode.Paused)
            {
                StartNotification();
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }
    }
예제 #2
0
        private void UpdatePlaybackState(String error)
        {
            var position = PlaybackState.PlaybackPositionUnknown;

            if (_musicPlayer != null)
            {
                position = _musicPlayer.CurrentStreamPosition;
            }

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

            SetCustomAction(stateBuilder);

            if (error != null)
            {
                stateBuilder.SetErrorMessage(error);
                stateBuilder.SetState(PlaybackStateCode.Error, position, 1.0f, SystemClock.ElapsedRealtime());
            }
            else
            {
                stateBuilder.SetState(_musicPlayer.MusicPlayerState, position, 1.0f, SystemClock.ElapsedRealtime());
            }

            if (this.isIndexPlayable(_currentIndexQueue, _playingQueue))
            {
                var item = _playingQueue[_currentIndexQueue];
                stateBuilder.SetActiveQueueItemId(item.QueueId);
            }

            _session.SetPlaybackState(stateBuilder.Build());
        }
예제 #3
0
        private void SetCustomAction(PlaybackState.Builder stateBuilder)
        {
            MediaMetadata currentMusic = GetCurrentPlayingMusic();

            if (currentMusic != null)
            {
                // Set appropriate "Favorite" icon on Custom action:
                var musicId      = currentMusic.GetString(MediaMetadata.MetadataKeyMediaId);
                var favoriteIcon = Resource.Drawable.ic_star_off;
                if (_musicProvider.IsFavorite(musicId))
                {
                    favoriteIcon = Resource.Drawable.ic_star_on;
                }
                stateBuilder.AddCustomAction(CustomActionFavorite, "Favorite", favoriteIcon);
            }
        }
예제 #4
0
        void SetCustomAction(PlaybackState.Builder stateBuilder)
        {
            MediaMetadata currentMusic = GetCurrentPlayingMusic();

            if (currentMusic != null)
            {
                // Set appropriate "Favorite" icon on Custom action:
                var musicId      = currentMusic.GetString(MediaMetadata.MetadataKeyMediaId);
                var favoriteIcon = Resource.Drawable.ic_star_off;
                if (musicProvider.IsFavorite(musicId))
                {
                    favoriteIcon = Resource.Drawable.ic_star_on;
                }
                LogHelper.Debug(Tag, "updatePlaybackState, setting Favorite custom action of music ",
                                musicId, " current favorite=", musicProvider.IsFavorite(musicId));
                stateBuilder.AddCustomAction(CustomActionThumbsUp, GetString(Resource.String.favorite),
                                             favoriteIcon);
            }
        }
예제 #5
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();
            }
        }
예제 #6
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);
			PlaybackStateCode 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 ();
			}
		}
        /// <summary>
        /// OnCreate() detects some of our managers
        /// </summary>
        public override void OnCreate()
        {
            Log.Debug(TAG, "StreamingService.OnCreate()");
            base.OnCreate();

            audioManager = (AudioManager)GetSystemService(AudioService);
            wifiManager = (WifiManager)GetSystemService(WifiService);
            headphonesUnpluggedReceiver = new MusicBroadcastReceiver();
            if (Android.OS.Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
            {
                mediaSession = new MediaSession(this, "KVMRMediaSession");

                mediaCallback = new MediaSessionCallback();
                mediaCallback.OnPlayImpl = () =>
                {
                    if (IsPlaying)
                    {
                        Log.Debug(TAG, "MediaCallback stop playing");
                        Stop();
                        MessagingCenter.Send<PlayerStoppedMessage>(new PlayerStoppedMessage(), "PlayerStopped");
                    }
                    else
                    {
                        Log.Debug(TAG, "MediaCallback start playing. source: " + source ?? "null");
                        Play(source);
                        MessagingCenter.Send(new RemoteControlPlayMessage(), "RemoteControlPlay");
                    }
                };

                mediaSession.SetCallback(mediaCallback);
                mediaSession.SetFlags(MediaSessionFlags.HandlesMediaButtons |
                    MediaSessionFlags.HandlesTransportControls);

                PlaybackState state = new PlaybackState.Builder()
                    .SetActions(PlaybackState.ActionPlay | PlaybackState.ActionPlayPause
                                          | PlaybackState.ActionPause | PlaybackState.ActionStop)
                    .Build();
                mediaSession.SetPlaybackState(state);
                mediaSession.Active = true;
            }
        }