Esempio n. 1
0
        public void StartNotification()
        {
            if (started)
            {
                return;
            }
            notificationColor = ResourceHelper.GetThemeColor(Service.ApplicationContext, Resource.Attribute.colorPrimary, Color.DarkGray);
            metadata          = controller.Metadata;
            playbackState     = controller.PlaybackState;

            var notification = CreateNotification();

            if (notification == null)
            {
                return;
            }

            started = true;
            controller.RegisterCallback(callback);
            var filter = new IntentFilter();

            filter.AddAction(ActionNext);
            filter.AddAction(ActionPause);
            filter.AddAction(ActionPlay);
            filter.AddAction(ActionPrevious);
            filter.AddAction(ActionStopCasting);

            Service.RegisterReceiver(this, filter);
            Service.StartForeground(NotificationId, notification);
        }
Esempio n. 2
0
        private void OnPlaybackStateChanged(PlaybackStateCompat state)
        {
            var message = new PlaybackStateChangedMessage
            {
                State = (PlaybackState)state.State
            };

            MessagingCenter.Send(message, nameof(PlaybackStateChangedMessage));
        }
        /// <summary>
        /// Updates the state of the player.
        /// </summary>
        /// <param name="state">The state.</param>
        /// <param name="position"></param>
        public void UpdatePlaybackState(int state, int position = 0, string errorMessage = "")
        {
            if (CurrentSession == null && (_binder?.IsBinderAlive).GetValueOrDefault(false) && !string.IsNullOrWhiteSpace(_packageName))
            {
                InitMediaSession(_packageName, _binder);
            }

            PlaybackStateCompat.Builder stateBuilder = new PlaybackStateCompat.Builder()
                                                       .SetActions(PlaybackStateCompat.ActionPlay
                                                                   | PlaybackStateCompat.ActionPlayPause
                                                                   | PlaybackStateCompat.ActionPause
                                                                   | PlaybackStateCompat.ActionSkipToNext
                                                                   | PlaybackStateCompat.ActionSkipToPrevious
                                                                   | PlaybackStateCompat.ActionStop);

            stateBuilder.SetState(state, position, 0, SystemClock.ElapsedRealtime());
            if (state == PlaybackStateCompat.StateError)
            {
                stateBuilder.SetErrorMessage(errorMessage);
            }
            PlaybackStateCompat playbackStateCompat = stateBuilder.Build();

            try
            {
                CurrentSession?.SetPlaybackState(playbackStateCompat);
            }
            catch (ArgumentException)
            {
                if (state != PlaybackStateCompat.StateError)
                {
                    throw;
                }
            }
            OnStatusChanged?.Invoke(CurrentSession, state);

            //Used for backwards compatibility
            if ((Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop) &&
                (CurrentSession?.RemoteControlClient == null ||
                 (bool)!CurrentSession?.RemoteControlClient.Equals(typeof(RemoteControlClient))))
            {
                return;
            }

            RemoteControlClient remoteControlClient = (RemoteControlClient)CurrentSession?.RemoteControlClient;

            RemoteControlFlags flags = RemoteControlFlags.Play
                                       | RemoteControlFlags.Pause
                                       | RemoteControlFlags.PlayPause
                                       | RemoteControlFlags.FastForward;

            remoteControlClient?.SetTransportControlFlags(flags);
        }
Esempio n. 4
0
 public void OnPlaybackStateChanged(PlaybackStateCompat state)
 {
     playbackState = state;
     if (state.State == PlaybackStateCompat.StateStopped || state.State == PlaybackStateCompat.StateNone)
     {
         StopNotification();
     }
     else
     {
         var notification = CreateNotification();
         if (notification != null)
         {
             NotificationManager.Notify(NotificationId, notification);
         }
     }
 }
Esempio n. 5
0
 public void OnPlaybackStateChanged(PlaybackStateCompat state)
 {
     //Console.WriteLine("************");
     //Console.WriteLine("***************");
     //Console.WriteLine($"Base Activity: Playback State: {state.State} - position {state.Position}");
     //NativeAudioPlayer.Context?.Session?.SetPlaybackState(state);
     //Console.WriteLine("***************");
     //Console.WriteLine("******");
     if (ShouldShowControls())
     {
         ShowPlaybackControls();
     }
     else
     {
         HidePlaybackControls();
     }
 }
        private void OnPlaybackStateChanged(PlaybackStateCompat state)
        {
            LogHelper.Debug(Tag, "onPlaybackStateChanged ", state);
            if (Activity == null)
            {
                LogHelper.Warn(Tag, "onPlaybackStateChanged called when getActivity null," +
                               "this should not happen if the callback was properly unregistered. Ignoring.");
                return;
            }
            if (state == null)
            {
                return;
            }
            bool enablePlay = false;

            switch (state.State)
            {
            case PlaybackStateCompat.StatePaused:
            case PlaybackStateCompat.StateStopped:
                enablePlay = true;
                break;

            case PlaybackStateCompat.StateError:
                LogHelper.Error(Tag, "error playbackstate: ", state.ErrorMessage);
                Toast.MakeText(Activity, state.ErrorMessage, ToastLength.Long).Show();
                break;
            }

            if (enablePlay)
            {
                playPause.SetImageDrawable(
                    ContextCompat.GetDrawable(Activity, Resource.Drawable.ic_play_arrow_black_36dp));
            }
            else
            {
                playPause.SetImageDrawable(
                    ContextCompat.GetDrawable(Activity, Resource.Drawable.ic_pause_black_36dp));
            }
        }
Esempio n. 7
0
        public void OnPlaybackStateChanged(PlaybackStateCompat state)
        {
            if (Activity == null || state == null)
            {
                return;
            }
            var enablePlay = false;

            switch (state.State)
            {
            case PlaybackStateCompat.StatePaused:
            case PlaybackStateCompat.StateStopped:
                enablePlay = true;
                break;

            case PlaybackStateCompat.StateError:
                Toast.MakeText(Activity, state.ErrorMessageFormatted, ToastLength.Long).Show();
                break;
            }
            playPauseButton.SetImageDrawable(ContextCompat.GetDrawable(Activity, enablePlay ? Resource.Drawable.ic_play_arrow_black_36dp : Resource.Drawable.ic_pause_black_36dp));
            setExtraInfo();
        }
Esempio n. 8
0
        private void CreateMediaSession()
        {
            mediaSessionCompatCallback = new MediaSessionCompatCallbacks(this);

            mediaSessionCompat = new MediaSessionCompat(context, channelId);

            Android.Support.V4.Media.MediaMetadataCompat.Builder mediaMetadataCompat = new Android.Support.V4.Media.MediaMetadataCompat.Builder();
            mediaMetadataCompat.PutString(Android.Support.V4.Media.MediaMetadataCompat.MetadataKeyArtist, AppResources.UnknownArtist);
            mediaMetadataCompat.PutString(Android.Support.V4.Media.MediaMetadataCompat.MetadataKeyTitle, AppResources.UnknownTitle);
            mediaSessionCompat.SetMetadata(mediaMetadataCompat.Build());
            mediaSessionCompat.SetMediaButtonReceiver(PendingIntent.GetBroadcast(context, 5, mediaButtonReceiverIntent, PendingIntentFlags.CancelCurrent)); //TODO: Not working

            playbackStateCompat = new PlaybackStateCompat.Builder().SetActions(PlaybackStateCompat.ActionStop | PlaybackStateCompat.ActionPlay | PlaybackStateCompat.ActionPause | PlaybackStateCompat.ActionPlayPause | PlaybackStateCompat.ActionSkipToNext | PlaybackStateCompat.ActionSkipToPrevious)
                                  .SetState(PlaybackStateCompat.StateBuffering, PlaybackStateCompat.PlaybackPositionUnknown, 0)
                                  .Build();

            mediaPlayer = MediaPlayer.Create(context, Resource.Raw.silence);
            mediaPlayer.Start();

            mediaSessionCompat.SetPlaybackState(playbackStateCompat);
            mediaSessionCompat.SetCallback(mediaSessionCompatCallback);

            mediaSessionCompat.Active = true;
        }
        public void StartNotification()
        {
            if (!started)
            {
                metadata      = controller.Metadata;
                playbackState = controller.PlaybackState;

                // The notification must be updated after setting started to true
                Notification notification = CreateNotification();
                if (notification != null)
                {
                    controller.RegisterCallback(mCb);
                    var filter = new IntentFilter();
                    filter.AddAction(ActionNext);
                    filter.AddAction(ActionPause);
                    filter.AddAction(ActionPlay);
                    filter.AddAction(ActionPrev);
                    service.RegisterReceiver(this, filter);

                    service.StartForeground(NotificationId, notification);
                    started = true;
                }
            }
        }
Esempio n. 10
0
 public static MediaPlayerState ToMediaPlayerState(this PlaybackStateCompat playbackState)
 {
     return(ToMediaPlayerState(playbackState.State));
 }
 public override void OnPlaybackStateChanged(PlaybackStateCompat state)
 {
     base.OnPlaybackStateChanged(state);
     OnPlaybackStateChangedImpl?.Invoke(state);
 }
Esempio n. 12
0
 public override void OnPlaybackStateChanged(PlaybackStateCompat state)
 {
     OnPlaybackStateChangedImpl(state);
 }
Esempio n. 13
0
 public override void OnPlaybackStateChanged(PlaybackStateCompat state)
 {
     Parent?.OnPlaybackStateChanged(state);
 }
Esempio n. 14
0
 public void OnPlaybackStateUpdated(PlaybackStateCompat newState)
 {
     session.SetPlaybackState(newState);
 }
        public MediaNotificationManager(MusicService serv)
        {
            service = serv;
            UpdateSessionToken();

            notificationColor = ResourceHelper.GetThemeColor(service,
                                                             Android.Resource.Attribute.ColorPrimary, Color.DarkGray);

            notificationManager = NotificationManagerCompat.From(service);

            string pkg = service.PackageName;

            pauseIntent = PendingIntent.GetBroadcast(service, RequestCode,
                                                     new Intent(ActionPause).SetPackage(pkg), PendingIntentFlags.CancelCurrent);
            playIntent = PendingIntent.GetBroadcast(service, RequestCode,
                                                    new Intent(ActionPlay).SetPackage(pkg), PendingIntentFlags.CancelCurrent);
            previousIntent = PendingIntent.GetBroadcast(service, RequestCode,
                                                        new Intent(ActionPrev).SetPackage(pkg), PendingIntentFlags.CancelCurrent);
            nextIntent = PendingIntent.GetBroadcast(service, RequestCode,
                                                    new Intent(ActionNext).SetPackage(pkg), PendingIntentFlags.CancelCurrent);

            notificationManager.CancelAll();

            mCb.OnPlaybackStateChangedImpl = (state) => {
                playbackState = state;
                LogHelper.Debug(Tag, "Received new playback state", state);
                if (state.State == PlaybackStateCompat.StateStopped ||
                    state.State == PlaybackStateCompat.StateNone)
                {
                    StopNotification();
                }
                else
                {
                    Notification notification = CreateNotification();
                    if (notification != null)
                    {
                        notificationManager.Notify(NotificationId, notification);
                    }
                }
            };

            mCb.OnMetadataChangedImpl = (meta) => {
                metadata = meta;
                LogHelper.Debug(Tag, "Received new metadata ", metadata);
                Notification notification = CreateNotification();
                if (notification != null)
                {
                    notificationManager.Notify(NotificationId, notification);
                }
            };

            mCb.OnSessionDestroyedImpl = () => {
                LogHelper.Debug(Tag, "Session was destroyed, resetting to the new session token");
                try
                {
                    UpdateSessionToken();
                }
                catch (Exception e)
                {
                    LogHelper.Error(Tag, e, "could not connect media controller");
                }
            };
        }