Пример #1
0
        private void UpdateMediaMetadataCompat(MediaMetadataRetriever metaRetriever = null)
        {
            if (_mediaSessionCompat != null)
            {
                MainActivity.Instance.RunOnUiThread(() =>
                {
                    MediaMetadataCompat.Builder builder = new MediaMetadataCompat.Builder();
                    var item = _mediaSessionCompat.SessionToken;
                    if (metaRetriever != null)
                    {
                        builder
                        .PutString(MediaMetadata.MetadataKeyAlbum, metaRetriever.ExtractMetadata(MetadataKey.Album))
                        .PutString(MediaMetadata.MetadataKeyArtist, metaRetriever.ExtractMetadata(MetadataKey.Artist))
                        .PutString(MediaMetadata.MetadataKeyDisplayTitle, metaRetriever.ExtractMetadata(MetadataKey.Title));
                    }
                    else
                    {
                        builder
                        .PutString(MediaMetadata.MetadataKeyAlbum, _queue[_pos].Album)
                        .PutString(MediaMetadata.MetadataKeyArtist, _queue[_pos].Artist)
                        .PutString(MediaMetadata.MetadataKeyDisplayTitle, _queue[_pos].Title);
                    }

                    if (!String.IsNullOrEmpty(_queue[_pos].Image.ToString()))
                    {
                        Bitmap artwork = BitmapFactory.DecodeFile(_queue[_pos].Image.ToString());
                        builder.PutBitmap(MediaMetadataCompat.MetadataKeyAlbumArt, artwork);
                        builder.PutBitmap(MediaMetadataCompat.MetadataKeyArt, artwork);
                    }

                    _mediaSessionCompat.SetMetadata(builder.Build());
                });
            }
        }
Пример #2
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());
            }
        }
Пример #3
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;
        }
Пример #4
0
        public static async void UpdateChromecastNotification(string title, string body, bool isPaused, string poster, long time, long duration)
        {
            try {
                //new Android.Support.V4.Media.Session.MediaControllerCompat.
                var builder = new AndroidX.Core.App.NotificationCompat.Builder(Application.Context);
                builder.SetContentTitle(title);
                builder.SetContentText(body);
                builder.SetAutoCancel(false);

                builder.SetSmallIcon(Resource.Drawable.round_cast_white_48dp2_4);
                builder.SetOngoing(true);

                var context = MainActivity.activity.ApplicationContext;
                if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
                {
                    var channelId = $"{PkgName}.general";
                    var channel   = new NotificationChannel(channelId, "General", NotificationImportance.Default);

                    NotManager.CreateNotificationChannel(channel);

                    builder.SetChannelId(channelId);
                    //https://m.media-amazon.com/images/M/MV5BMTczNTI2ODUwOF5BMl5BanBnXkFtZTcwMTU0NTIzMw@@._V1_UX182_CR0,0,182,268_AL_.jpg
                    var bitmap = await GetImageBitmapFromUrl(poster);                    //"https://m.media-amazon.com/images/M/MV5BMTczNTI2ODUwOF5BMl5BanBnXkFtZTcwMTU0NTIzMw@@._V1_UX182_CR0,0,182,268_AL_.jpg");

                    if (bitmap != null)
                    {
                        builder.SetLargeIcon(bitmap);
                    }

                    var stateBuilder = new PlaybackStateCompat.Builder()
                                       .SetActions(PlaybackStateCompat.ActionSeekTo | PlaybackStateCompat.ActionPause | PlaybackStateCompat.ActionPlay | PlaybackStateCompat.ActionStop)
                                       .SetState(
                        isPaused ? PlaybackStateCompat.StatePaused : PlaybackStateCompat.StatePlaying,
                        time, 1f, SystemClock.ElapsedRealtime()
                        );

                    mediaSession.SetPlaybackState(stateBuilder.Build());
                    mediaSession.SetFlags(MediaSessionCompat.FlagHandlesMediaButtons | MediaSessionCompat.FlagHandlesTransportControls);
                    mediaSession.SetMetadata(new Android.Support.V4.Media.MediaMetadataCompat.Builder()
                                                                                                   //.PutString(MediaMetadata.MetadataKeyArtist, "title")
                                                                                                   //.PutString(MediaMetadata.MetadataKeyTitle, "genre")
                                             .PutLong(MediaMetadata.MetadataKeyDuration, duration) //Negative duration means the duration is unknown
                                                                                                   // .PutString(MediaMetadata.MetadataKeyArt, "https://homepages.cae.wisc.edu/~ece533/images/peppers.png")
                                             .Build());
                    builder.SetColor(Android.Graphics.Color.Black.ToArgb());                       // THIS IS VERY IMPORTANT FOR THE APPERANCE OF THE SEEKBAR IN THE NOTIFICATION
                    builder.SetColorized(true);

                    mediaSession.Active = true;

                    //	mediaSession.SetMetadata(new Android.Support.V4.Media.MediaMetadataCompat() { Description = new Android.Support.V4.Media.MediaDescriptionCompat() { } })

                    List <string> actionNames = new List <string>()
                    {
                        "-30s", isPaused ? "Play" : "Pause", "+30s", "Stop"
                    };
                    List <int> sprites = new List <int>()
                    {
                        Resource.Drawable.netflixGoBack128, isPaused ? Resource.Drawable.netflixPlay128v2 : Resource.Drawable.netflixPause128v2, Resource.Drawable.netflixGoForward128, Resource.Drawable.netflixStop128v2
                    };
                    List <string> actionIntent = new List <string>()
                    {
                        "goback", isPaused ? "play" : "pause", "goforward", "stop"
                    };                                                                                                                                 // next

                    for (int i = 0; i < sprites.Count; i++)
                    {
                        var _resultIntent = new Intent(context, typeof(ChromeCastIntentService));
                        _resultIntent.PutExtra("data", actionIntent[i]);
                        var _pending = PendingIntent.GetService(context, 2337 + i,
                                                                _resultIntent,
                                                                PendingIntentFlags.UpdateCurrent
                                                                );

                        builder.AddAction(new AndroidX.Core.App.NotificationCompat.Action(sprites[i], actionNames[i], _pending));
                    }

                    builder.SetStyle(new NotificationCompat.MediaStyle().SetMediaSession(mediaSession.SessionToken).SetShowActionsInCompactView(0, 1, 2));                     // NICER IMAGE
                }

                builder.SetContentIntent(GetCurrentPending("openchrome"));

                try {
                    NotManager.Notify(CHROME_CAST_NOTIFICATION_ID, builder.Build());
                }
                catch (Exception _ex) {
                    print("EX NOTTIFY;; " + _ex);
                }
            }
            catch (Exception _ex) {
                error(_ex);
            }
        }