Exemplo n.º 1
0
        protected override void OnStart()
        {
            base.OnStart();
            LogHelper.Debug(Tag, "Activity onStart");

            controlsFragment = FragmentManager.FindFragmentById <PlaybackControlsFragment>(Resource.Id.fragment_playback_controls);
            if (controlsFragment == null)
            {
                throw new Exception("Mising fragment with id 'controls'. Cannot continue.");
            }

            HidePlaybackControls();

            var permissionsList = new List <string>();

            if (ActivityCompat.CheckSelfPermission(this, Manifest.Permission.ReadExternalStorage) == Permission.Granted)
            {
                mediaBrowser.Connect();
            }
            else
            {
                permissionsList.Add(Manifest.Permission.ReadExternalStorage);
            }

            if (ActivityCompat.CheckSelfPermission(this, Manifest.Permission.RecordAudio) != Permission.Granted)
            {
                permissionsList.Add(Manifest.Permission.RecordAudio);
            }

            if (permissionsList.Count > 0)
            {
                ActivityCompat.RequestPermissions(this, permissionsList.ToArray(), RequestCodeAskPermissions);
                return;
            }
        }
Exemplo n.º 2
0
 void WireUpMediaBrowser()
 {
     MessagingCenter.Subscribe <MediaBrowserConnectMessage>(this, nameof(MediaBrowserConnectMessage), message =>
     {
         if (mediaBrowser.IsConnected)
         {
             OnConnected();
         }
         else
         {
             mediaBrowser.Connect();
         }
     });
 }
        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);
        }