예제 #1
0
        public void UpdateWidget(string artist, string title, byte[] thumbnail)
        {
            if (notification != null && mediaSessionCompat != null)
            {
                AndroidX.Core.App.NotificationCompat.Builder builder = new AndroidX.Core.App.NotificationCompat.Builder(context, channelId);
                builder.SetVisibility(NotificationCompat.VisibilityPublic)
                .SetSmallIcon(Resource.Mipmap.icon)
                .AddAction(Resource.Drawable.ic_volume_down, "VolumeDown", PendingIntent.GetBroadcast(context, 0, volumeDownIntent, PendingIntentFlags.CancelCurrent))     // #0
                .AddAction(Resource.Drawable.ic_skip_previous, "Previous", PendingIntent.GetBroadcast(context, 1, previousIntent, PendingIntentFlags.CancelCurrent))       // #1
                .AddAction(Resource.Drawable.ic_play_circle_outline, "PlayStop", PendingIntent.GetBroadcast(context, 2, playStopIntent, PendingIntentFlags.CancelCurrent)) // #2
                .AddAction(Resource.Drawable.ic_skip_next, "Next", PendingIntent.GetBroadcast(context, 3, nextIntent, PendingIntentFlags.CancelCurrent))                   // #3
                .AddAction(Resource.Drawable.ic_volume_up, "VolumeUp", PendingIntent.GetBroadcast(context, 4, volumeUpIntent, PendingIntentFlags.CancelCurrent))           // #4
                .SetStyle(new AndroidX.Media.App.NotificationCompat.MediaStyle().SetShowActionsInCompactView(2 /* #2: pause button */).SetMediaSession(mediaSessionCompat.SessionToken))
                .SetOngoing(true)
                .SetSilent(true)
                .SetVibrate(new long[] { 0L });

                Android.Support.V4.Media.MediaMetadataCompat.Builder mediaMetadataCompat = new Android.Support.V4.Media.MediaMetadataCompat.Builder();

                if (artist.Replace("\0", "") == "")
                {
                    builder.SetContentTitle(AppResources.UnknownArtist);
                    mediaMetadataCompat.PutString(Android.Support.V4.Media.MediaMetadataCompat.MetadataKeyArtist, AppResources.UnknownArtist);
                }
                else
                {
                    builder.SetContentTitle(artist);
                    mediaMetadataCompat.PutString(Android.Support.V4.Media.MediaMetadataCompat.MetadataKeyArtist, artist);
                }

                if (title.Replace("\0", "") == "")
                {
                    builder.SetContentText(AppResources.UnknownTitle);
                    mediaMetadataCompat.PutString(Android.Support.V4.Media.MediaMetadataCompat.MetadataKeyTitle, AppResources.UnknownTitle);
                }
                else
                {
                    builder.SetContentText(title);
                    mediaMetadataCompat.PutString(Android.Support.V4.Media.MediaMetadataCompat.MetadataKeyTitle, title);
                }

                Bitmap bitmap = null;
                if (thumbnail != null)
                {
                    bitmap = BitmapFactory.DecodeByteArray(thumbnail, 0, thumbnail.Length);
                }
                builder.SetLargeIcon(bitmap);

                notification = builder.Build();
                notificationManager.Notify(messageId, notification);

                mediaSessionCompat.SetMetadata(mediaMetadataCompat.Build());
            }
        }
예제 #2
0
        private void CreateMediaSession()
        {
            mediaSessionCompatCallback = new MediaSessionCompatCallbacks(this);

            mediaSessionCompat = new MediaSessionCompat(context, channelId);

            Android.Support.V4.Media.MediaMetadataCompat.Builder mediaMetadataCompat = new Android.Support.V4.Media.MediaMetadataCompat.Builder();
            mediaMetadataCompat.PutString(Android.Support.V4.Media.MediaMetadataCompat.MetadataKeyArtist, AppResources.UnknownArtist);
            mediaMetadataCompat.PutString(Android.Support.V4.Media.MediaMetadataCompat.MetadataKeyTitle, AppResources.UnknownTitle);
            mediaSessionCompat.SetMetadata(mediaMetadataCompat.Build());
            mediaSessionCompat.SetMediaButtonReceiver(PendingIntent.GetBroadcast(context, 5, mediaButtonReceiverIntent, PendingIntentFlags.CancelCurrent)); //TODO: Not working

            playbackStateCompat = new PlaybackStateCompat.Builder().SetActions(PlaybackStateCompat.ActionStop | PlaybackStateCompat.ActionPlay | PlaybackStateCompat.ActionPause | PlaybackStateCompat.ActionPlayPause | PlaybackStateCompat.ActionSkipToNext | PlaybackStateCompat.ActionSkipToPrevious)
                                  .SetState(PlaybackStateCompat.StateBuffering, PlaybackStateCompat.PlaybackPositionUnknown, 0)
                                  .Build();

            mediaPlayer = MediaPlayer.Create(context, Resource.Raw.silence);
            mediaPlayer.Start();

            mediaSessionCompat.SetPlaybackState(playbackStateCompat);
            mediaSessionCompat.SetCallback(mediaSessionCompatCallback);

            mediaSessionCompat.Active = true;
        }