protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); LogHelper.Debug(Tag, "Activity onCreate"); // 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. mediaBrowser = new MediaBrowserCompat(this, new ComponentName(this, Java.Lang.Class.FromType(typeof(MusicService))), connectionCallback, null); mediaControllerCallback.OnPlaybackStateChangedImpl = (state) => { if (ShouldShowControls) { ShowPlaybackControls(); } else { LogHelper.Debug(Tag, "mediaControllerCallback.onPlaybackStateChanged: " + "hiding controls because state is ", state.State); HidePlaybackControls(); } }; mediaControllerCallback.OnMetadataChangedImpl = (metadata) => { if (ShouldShowControls) { ShowPlaybackControls(); } else { LogHelper.Debug(Tag, "mediaControllerCallback.onMetadataChanged: " + "hiding controls because metadata is null"); HidePlaybackControls(); } }; connectionCallback.OnConnectedImpl = () => { LogHelper.Debug(Tag, "onConnected"); try { ConnectToSession(mediaBrowser.SessionToken); } catch (RemoteException e) { LogHelper.Error(Tag, e, "could not connect media controller"); HidePlaybackControls(); } }; }
protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); if ((int)Build.VERSION.SdkInt >= 21) { //TODO: Fix color var taskDesc = new ActivityManager.TaskDescription(Title, BitmapFactory.DecodeResource(this.Resources, Resource.Drawable.ic_launcher_white), Color.DarkGray); SetTaskDescription(taskDesc); } MediaBrowser = new MediaBrowserCompat(this, new ComponentName(this, Java.Lang.Class.FromType((typeof(MusicService)))), new MediaBrowserCallBack { Parent = this }, null); }
protected override void OnCreate(Bundle bundle) { TabLayoutResource = Resource.Layout.Tabbar; ToolbarResource = Resource.Layout.Toolbar; base.OnCreate(bundle); global::Xamarin.Forms.Forms.Init(this, bundle); LoadApplication(new App()); // 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. mediaBrowser = new MediaBrowserCompat(this, new ComponentName(this, Java.Lang.Class.FromType(typeof(MusicService))), connectionCallback, null); mediaControllerCallback.OnPlaybackStateChangedImpl = (state) => { if (ShouldShowControls) { ShowPlaybackControls(); } else { LogHelper.Debug(Tag, "mediaControllerCallback.onPlaybackStateChanged: " + "hiding controls because state is ", state.State); HidePlaybackControls(); } LogHelper.Debug(Tag, "Received playback state change to state ", state.State); OnPlaybackStateChanged(state); }; mediaControllerCallback.OnMetadataChangedImpl = (metadata) => { if (ShouldShowControls) { ShowPlaybackControls(); } else { LogHelper.Debug(Tag, "mediaControllerCallback.onMetadataChanged: " + "hiding controls because metadata is null"); HidePlaybackControls(); } if (metadata != null) { LogHelper.Debug(Tag, "Received metadata state change to mediaId=", metadata.Description.MediaId, " song=", metadata.Description.Title); OnMetadataChanged(metadata); } }; connectionCallback.OnConnectedImpl = () => { LogHelper.Debug(Tag, "onConnected"); try { ConnectToSession(mediaBrowser.SessionToken); } catch (RemoteException e) { LogHelper.Error(Tag, e, "could not connect media controller"); HidePlaybackControls(); } }; subscriptionCallback.OnChildrenLoadedImpl = (parentId, children) => { OnChildrenLoaded(children); }; subscriptionCallback.OnErrorImpl = (id) => { LogHelper.Error(Tag, "browse subscription onError, id=" + id); }; WireUpMediaBrowser(); WireUpPlayMediaItemTask(); }
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); }