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);
        }
Exemple #2
0
        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            View rootView = inflater.Inflate(Resource.Layout.fragment_list, container, false);

            browserAdapter = new BrowseAdapter(Activity);

            View controls = rootView.FindViewById(Resource.Id.controls);

            controls.Visibility = ViewStates.Gone;

            var listView = rootView.FindViewById <ListView> (Resource.Id.list_view);

            listView.Adapter = browserAdapter;

            listView.ItemClick += (sender, e) => {
                MediaBrowser.MediaItem item = browserAdapter.GetItem(e.Position);
                try {
                    var listener = (IFragmentDataHelper)Activity;
                    listener.OnMediaItemSelected(item);
                } catch (InvalidCastException ex) {
                    Log.Error(Tag, "Exception trying to cast to FragmentDataHelper", ex);
                }
            };

            Bundle args = Arguments;

            mediaId      = args.GetString(ArgMediaId, null);
            mediaBrowser = new MediaBrowser(Activity,
                                            new ComponentName(Activity, Java.Lang.Class.FromType(typeof(MusicService))),
                                            connectionCallback, null);

            subscriptionCallback.OnChildrenLoadedImpl = (parentId, children) => {
                browserAdapter.Clear();
                browserAdapter.NotifyDataSetInvalidated();
                foreach (MediaBrowser.MediaItem item in children)
                {
                    browserAdapter.Add(item);
                }
                browserAdapter.NotifyDataSetChanged();
            };

            subscriptionCallback.OnErrorImpl   = (id) => Toast.MakeText(Activity, "Error Loading Media", ToastLength.Long).Show();
            connectionCallback.OnConnectedImpl = () => {
                LogHelper.Debug(Tag, "onConnected: session token " + mediaBrowser.SessionToken);
                if (mediaId == null)
                {
                    mediaId = mediaBrowser.Root;
                }
                mediaBrowser.Subscribe(mediaId, subscriptionCallback);
                if (mediaBrowser.SessionToken == null)
                {
                    throw new ArgumentNullException("No Session token");
                }
                var mediaController = new Android.Media.Session.MediaController(Activity, mediaBrowser.SessionToken);
                Activity.MediaController = mediaController;
            };
            connectionCallback.OnConnectionFailedImpl    = () => LogHelper.Debug(Tag, "onConnectionFailed");
            connectionCallback.OnConnectionSuspendedImpl = () => {
                LogHelper.Debug(Tag, "onConnectionSuspended");
                Activity.MediaController = null;
            };
            return(rootView);
        }