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);
        }
Exemple #2
0
        void ConnectToSession(MediaSessionCompat.Token token)
        {
            var controller = new MediaControllerCompat(this, token);

            this.SupportMediaController          = controller;
            controller.RegisterCallback(callBack = new MediaControllerCallBack {
                Parent = this
            });
            if (ShouldShowControls())
            {
                ShowPlaybackControls();
            }
            else
            {
                HidePlaybackControls();
            }
            ControlsFragment?.OnConnected();
            OnMediaControllConnected();
        }
        void UpdateSessionToken()
        {
            var freshToken = Service.SessionToken;

            if (freshToken == null || sessionToken == freshToken)
            {
                return;
            }

            controller?.UnregisterCallback(callback);

            sessionToken      = freshToken;
            controller        = new MediaControllerCompat(Service, sessionToken);
            transportControls = controller.GetTransportControls();

            if (started)
            {
                controller.RegisterCallback(callback);
            }
        }
        void UpdateSessionToken()
        {
            var freshToken = service.SessionToken;

            if (sessionToken == null && freshToken != null ||
                sessionToken != null && sessionToken != freshToken)
            {
                controller?.UnregisterCallback(mCb);

                sessionToken = freshToken;
                if (sessionToken != null)
                {
                    controller        = new MediaControllerCompat(service, sessionToken);
                    transportControls = controller.GetTransportControls();
                    if (started)
                    {
                        controller.RegisterCallback(mCb);
                    }
                }
            }
        }
Exemple #5
0
        private void ConnectToSession(MediaSessionCompat.Token token)
        {
            var mediaController = new MediaControllerCompat(this, token);

            SupportMediaController = mediaController;
            mediaController.RegisterCallback(mediaControllerCallback);

            if (ShouldShowControls)
            {
                ShowPlaybackControls();
            }
            else
            {
                LogHelper.Debug(Tag, "connectionCallback.onConnected: " +
                                "hiding controls because metadata is null");
                HidePlaybackControls();
            }

            controlsFragment?.OnConnected();

            OnMediaControllerConnected();
        }
        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;
                }
            }
        }
        public bool Init()
        {
            if (MediaBrowser == null)
            {
                MediaControllerCallback          = new MediaControllerCallback();
                MediaBrowserSubscriptionCallback = new MediaBrowserSubscriptionCallback();

                // Connect a media browser just to get the media session token. There are other ways
                // this can be done, for example by sharing the session token directly.
                TaskCompletionSource <bool> tcs = new TaskCompletionSource <bool>();
                MediaBrowserConnectionCallback = new MediaBrowserConnectionCallback
                {
                    OnConnectedImpl = () =>
                    {
                        MediaControllerCallback.OnMetadataChangedImpl = metadata =>
                        {
                            var test = metadata;
                        };

                        MediaControllerCallback.OnPlaybackStateChangedImpl = state =>
                        {
                            MediaManager.OnStateChanged(this, new StateChangedEventArgs(state.ToMediaPlayerState()));
                        };

                        MediaControllerCallback.OnSessionEventChangedImpl = (string @event, Bundle extras) =>
                        {
                            //Do nothing for now
                        };

                        MediaController = new MediaControllerCompat(Context, MediaBrowser.SessionToken);
                        MediaController.RegisterCallback(MediaControllerCallback);

                        if (Context is Activity activity)
                        {
                            MediaControllerCompat.SetMediaController(activity, MediaController);
                        }

                        // Sync existing MediaSession state to the UI.
                        // The first time these events are fired, the metadata and playbackstate are null.
                        MediaControllerCallback.OnMetadataChanged(MediaController.Metadata);
                        MediaControllerCallback.OnPlaybackStateChanged(MediaController.PlaybackState);

                        MediaBrowser.Subscribe(MediaBrowser.Root, MediaBrowserSubscriptionCallback);

                        IsInitialized = true;
                        tcs.SetResult(IsInitialized);
                    },

                    OnConnectionFailedImpl = () =>
                    {
                        IsInitialized = false;
                        tcs.SetResult(IsInitialized);
                    }
                };

                MediaBrowser = new MediaBrowserCompat(Context,
                                                      new ComponentName(
                                                          Context,
                                                          ServiceType),
                                                      MediaBrowserConnectionCallback,
                                                      null);
            }

            if (!IsInitialized)
            {
                MediaBrowser.Connect();
                IsInitialized = true;
            }

            return(IsInitialized);
        }