public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            View rootView = inflater.Inflate(Resource.Layout.media_player, container, false);

            _browserAdapter = new BrowseAdapter(Activity);

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

            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);
                var listener = (IMediaSelected)Activity;
                listener.OnMediaItemSelected(item);
            };

            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 = () => {
                Logger.Debug($"OnConnectedImpl: session token {_mediaBrowser.SessionToken}");
                if (string.IsNullOrEmpty(_mediaId))
                {
                    _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    = () => Logger.Debug("OnConnectionFailedImpl");
            _connectionCallback.OnConnectionSuspendedImpl = () =>
            {
                Logger.Debug("OnConnectionSuspendedImpl");
                Activity.MediaController = null;
            };
            return(rootView);
        }
        QueueFragment()
        {
            connectionCallback.OnConnectedImpl = () => {
                LogHelper.Debug(Tag, "onConnected: session token ", mediaBrowser.SessionToken);

                if (mediaBrowser.SessionToken == null)
                {
                    throw new InvalidOperationException("No Session token");
                }

                mediaController = new Android.Media.Session.MediaController(Activity,
                                                                            mediaBrowser.SessionToken);
                transportControls = mediaController.GetTransportControls();
                mediaController.RegisterCallback(sessionCallback);

                Activity.MediaController = mediaController;
                playbackState            = mediaController.PlaybackState;

                var queue = (JavaList)mediaController.Queue;
                if (queue != null)
                {
                    queueAdapter.Clear();
                    queueAdapter.NotifyDataSetInvalidated();
                    queueAdapter.AddAll(queue.ToArray());
                    queueAdapter.NotifyDataSetChanged();
                }
                OnPlaybackStateChanged(playbackState);
            };

            connectionCallback.OnConnectionFailedImpl    = () => LogHelper.Debug(Tag, "onConnectionFailed");
            connectionCallback.OnConnectionSuspendedImpl = () => {
                LogHelper.Debug(Tag, "onConnectionSuspended");
                mediaController.UnregisterCallback(sessionCallback);
                transportControls        = null;
                mediaController          = null;
                Activity.MediaController = null;
            };

            sessionCallback.OnSessionDestroyedImpl     = () => LogHelper.Debug(Tag, "Session destroyed. Need to fetch a new Media Session");
            sessionCallback.OnPlaybackStateChangedImpl = state => {
                if (state == null)
                {
                    return;
                }
                LogHelper.Debug(Tag, "Received playback state change to state ", state.State);
                playbackState = state;
                OnPlaybackStateChanged(state);
            };

            sessionCallback.OnQueueChangedImpl = queue => {
                LogHelper.Debug(Tag, "onQueueChanged ", queue);
                if (queue != null)
                {
                    queueAdapter.Clear();
                    queueAdapter.NotifyDataSetInvalidated();
                    queueAdapter.AddAll(queue.ToArray());
                    queueAdapter.NotifyDataSetChanged();
                }
            };
        }
Ejemplo n.º 3
0
        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            var rootView = inflater.Inflate(Resource.Layout.fragment_list, container, false);

            browserAdapter = new BrowseAdapter(Activity);

            var 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) => {
                var 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);
                }
            };
            var 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);
        }
Ejemplo n.º 4
0
        public MediaPlayerFragment()
        {
            _connectionCallback = new ConnectionCallback();
            _sessionCallback    = new SessionCallback();
            _connectionCallback.OnConnectedImpl = () =>
            {
                if (_mediaBrowser.SessionToken == null)
                {
                    throw new InvalidOperationException("No Session token");
                }

                _mediaController = new Android.Media.Session.MediaController(Activity,
                                                                             _mediaBrowser.SessionToken);
                _transportControls = _mediaController.GetTransportControls();
                _mediaController.RegisterCallback(_sessionCallback);

                Activity.MediaController = _mediaController;
                _playbackState           = _mediaController.PlaybackState;

                var queue = (JavaList)_mediaController.Queue;
                if (queue != null)
                {
                    _queueAdapter.Clear();
                    _queueAdapter.NotifyDataSetInvalidated();
                    _queueAdapter.AddAll(queue.ToArray());
                    _queueAdapter.NotifyDataSetChanged();
                }
                OnPlaybackStateChanged(_playbackState);
            };
            _connectionCallback.OnConnectionFailedImpl    = () => Logger.Debug("OnConnectionFailedImpl");
            _connectionCallback.OnConnectionSuspendedImpl = () =>
            {
                _mediaController.UnregisterCallback(_sessionCallback);
                _transportControls       = null;
                _mediaController         = null;
                Activity.MediaController = null;
            };
            _sessionCallback.OnSessionDestroyedImpl     = () => Logger.Debug("OnSessionDestroyedImpl");
            _sessionCallback.OnPlaybackStateChangedImpl = state =>
            {
                if (state == null)
                {
                    return;
                }
                _playbackState = state;
                OnPlaybackStateChanged(state);
            };
            _sessionCallback.OnQueueChangedImpl = queue =>
            {
                if (queue != null)
                {
                    _queueAdapter.Clear();
                    _queueAdapter.NotifyDataSetInvalidated();
                    _queueAdapter.AddAll(queue.ToArray());
                    _queueAdapter.NotifyDataSetChanged();
                }
            };
        }
Ejemplo n.º 5
0
		QueueFragment ()
		{
			connectionCallback.OnConnectedImpl = () => {
				LogHelper.Debug (Tag, "onConnected: session token ", mediaBrowser.SessionToken);

				if (mediaBrowser.SessionToken == null) {
					throw new InvalidOperationException ("No Session token");
				}

				mediaController = new Android.Media.Session.MediaController (Activity,
					mediaBrowser.SessionToken);
				transportControls = mediaController.GetTransportControls ();
				mediaController.RegisterCallback (sessionCallback);

				Activity.MediaController = mediaController;
				playbackState = mediaController.PlaybackState;

				var queue = (JavaList)mediaController.Queue;
				if (queue != null) {
					queueAdapter.Clear ();
					queueAdapter.NotifyDataSetInvalidated ();
					queueAdapter.AddAll(queue.ToArray());
					queueAdapter.NotifyDataSetChanged ();
				}
				OnPlaybackStateChanged (playbackState);
			};
			connectionCallback.OnConnectionFailedImpl = () => LogHelper.Debug (Tag, "onConnectionFailed");
			connectionCallback.OnConnectionSuspendedImpl = () => {
				LogHelper.Debug (Tag, "onConnectionSuspended");
				mediaController.UnregisterCallback (sessionCallback);
				transportControls = null;
				mediaController = null;
				Activity.MediaController = null;
			};
			sessionCallback.OnSessionDestroyedImpl = () => LogHelper.Debug (Tag, "Session destroyed. Need to fetch a new Media Session");
			sessionCallback.OnPlaybackStateChangedImpl = state => {
				if (state == null) {
					return;
				}
				LogHelper.Debug (Tag, "Received playback state change to state ", state.State);
				playbackState = state;
				OnPlaybackStateChanged (state);
			};
			sessionCallback.OnQueueChangedImpl = queue => {
				LogHelper.Debug (Tag, "onQueueChanged ", queue);
				if (queue != null) {
					queueAdapter.Clear ();
					queueAdapter.NotifyDataSetInvalidated ();
					queueAdapter.AddAll (queue.ToArray());
					queueAdapter.NotifyDataSetChanged ();
				}
			};
			buttonListener.OnClickImpl = v => {
				var state = playbackState == null ?
					PlaybackStateCode.None : playbackState.State;
				switch (v.Id) {
				case Resource.Id.play_pause:
					LogHelper.Debug (Tag, "Play button pressed, in state " + state);
					if (state == PlaybackStateCode.Paused ||
					    state == PlaybackStateCode.Stopped ||
					    state == PlaybackStateCode.None) {
						PlayMedia ();
					} else if (state == PlaybackStateCode.Playing) {
						PauseMedia ();
					}
					break;
				case Resource.Id.skip_previous:
					LogHelper.Debug (Tag, "Start button pressed, in state " + state);
					SkipToPrevious ();
					break;
				case Resource.Id.skip_next:
					SkipToNext ();
					break;
				}
			};
		}
Ejemplo n.º 6
0
		QueueFragment ()
		{
			connectionCallback.OnConnectedImpl = () => {
				LogHelper.Debug (Tag, "onConnected: session token ", mediaBrowser.SessionToken);

				if (mediaBrowser.SessionToken == null) {
					throw new InvalidOperationException ("No Session token");
				}

				mediaController = new Android.Media.Session.MediaController (Activity,
					mediaBrowser.SessionToken);
				transportControls = mediaController.GetTransportControls ();
				mediaController.RegisterCallback (sessionCallback);

				Activity.MediaController = mediaController;
				playbackState = mediaController.PlaybackState;

				var queue = (JavaList)mediaController.Queue;
				if (queue != null) {
					queueAdapter.Clear ();
					queueAdapter.NotifyDataSetInvalidated ();
					queueAdapter.AddAll(queue.ToArray());
					queueAdapter.NotifyDataSetChanged ();
				}
				OnPlaybackStateChanged (playbackState);
			};
			connectionCallback.OnConnectionFailedImpl = () => LogHelper.Debug (Tag, "onConnectionFailed");
			connectionCallback.OnConnectionSuspendedImpl = () => {
				LogHelper.Debug (Tag, "onConnectionSuspended");
				mediaController.UnregisterCallback (sessionCallback);
				transportControls = null;
				mediaController = null;
				Activity.MediaController = null;
			};
			sessionCallback.OnSessionDestroyedImpl = () => LogHelper.Debug (Tag, "Session destroyed. Need to fetch a new Media Session");
			sessionCallback.OnPlaybackStateChangedImpl = state => {
				if (state == null) {
					return;
				}
				LogHelper.Debug (Tag, "Received playback state change to state ", state.State);
				playbackState = state;
				OnPlaybackStateChanged (state);
			};
			sessionCallback.OnQueueChangedImpl = queue => {
				LogHelper.Debug (Tag, "onQueueChanged ", queue);
				if (queue != null) {
					queueAdapter.Clear ();
					queueAdapter.NotifyDataSetInvalidated ();
					queueAdapter.AddAll (queue.ToArray());
					queueAdapter.NotifyDataSetChanged ();
				}
			};
		}
Ejemplo n.º 7
0
        QueueFragment()
        {
            connectionCallback.OnConnectedImpl = () => {
                LogHelper.Debug(Tag, "onConnected: session token ", mediaBrowser.SessionToken);

                if (mediaBrowser.SessionToken == null)
                {
                    throw new InvalidOperationException("No Session token");
                }

                mediaController = new Android.Media.Session.MediaController(Activity,
                                                                            mediaBrowser.SessionToken);
                transportControls = mediaController.GetTransportControls();
                mediaController.RegisterCallback(sessionCallback);

                Activity.MediaController = mediaController;
                playbackState            = mediaController.PlaybackState;

                var queue = (JavaList)mediaController.Queue;
                if (queue != null)
                {
                    queueAdapter.Clear();
                    queueAdapter.NotifyDataSetInvalidated();
                    queueAdapter.AddAll(queue.ToArray());
                    queueAdapter.NotifyDataSetChanged();
                }
                OnPlaybackStateChanged(playbackState);
            };
            connectionCallback.OnConnectionFailedImpl    = () => LogHelper.Debug(Tag, "onConnectionFailed");
            connectionCallback.OnConnectionSuspendedImpl = () => {
                LogHelper.Debug(Tag, "onConnectionSuspended");
                mediaController.UnregisterCallback(sessionCallback);
                transportControls        = null;
                mediaController          = null;
                Activity.MediaController = null;
            };
            sessionCallback.OnSessionDestroyedImpl     = () => LogHelper.Debug(Tag, "Session destroyed. Need to fetch a new Media Session");
            sessionCallback.OnPlaybackStateChangedImpl = state => {
                if (state == null)
                {
                    return;
                }
                LogHelper.Debug(Tag, "Received playback state change to state ", state.State);
                playbackState = state;
                OnPlaybackStateChanged(state);
            };
            sessionCallback.OnQueueChangedImpl = queue => {
                LogHelper.Debug(Tag, "onQueueChanged ", queue);
                if (queue != null)
                {
                    queueAdapter.Clear();
                    queueAdapter.NotifyDataSetInvalidated();
                    queueAdapter.AddAll(queue.ToArray());
                    queueAdapter.NotifyDataSetChanged();
                }
            };
            buttonListener.OnClickImpl = v => {
                var state = playbackState == null ?
                            PlaybackStateCode.None : playbackState.State;
                switch (v.Id)
                {
                case Resource.Id.play_pause:
                    LogHelper.Debug(Tag, "Play button pressed, in state " + state);
                    if (state == PlaybackStateCode.Paused ||
                        state == PlaybackStateCode.Stopped ||
                        state == PlaybackStateCode.None)
                    {
                        PlayMedia();
                    }
                    else if (state == PlaybackStateCode.Playing)
                    {
                        PauseMedia();
                    }
                    break;

                case Resource.Id.skip_previous:
                    LogHelper.Debug(Tag, "Start button pressed, in state " + state);
                    SkipToPrevious();
                    break;

                case Resource.Id.skip_next:
                    SkipToNext();
                    break;
                }
            };
        }