Я создал уведомление для своего мультимедийного приложения, которое позволит пользователю воспроизводить/приостанавливать звук. Но прямо сейчас они могут нажать кнопку в уведомлении только один раз. Как я могу сделать так, чтобы пользователь мог постоянно нажимать кнопку воспроизведения/паузы?
Я попытался просто сделать новое уведомление в своей деятельности, чтобы просто обновить информацию, но это еще не дало мне особых результатов.
Создание уведомления
public void Init(DabPlayer Player, bool IntegrateWithLockScreen)
{
player = Player;
var mSession = new MediaSessionCompat(Application.Context, "MusicService");
mSession.SetFlags(MediaSessionCompat.FlagHandlesMediaButtons | MediaSessionCompat.FlagHandlesTransportControls);
var controller = mSession.Controller;
var description = GlobalResources.playerPodcast;
if (IntegrateWithLockScreen)
{
/* SET UP LOCK SCREEN */
CreateNotificationChannel();
player.EpisodeDataChanged += (sender, e) =>
{
// Set up an intent so that tapping the notifications returns to this app:
Intent intent = new Intent(Application.Context, typeof(MainActivity));
Intent playPauseIntent = new Intent(Application.Context, typeof(SecondActivity));
// Create a PendingIntent; we're only using one PendingIntent (ID = 0):
const int pendingIntentId = 0;
const int firstPendingIntentId = 1;
PendingIntent firstPendingIntent =
PendingIntent.GetActivity(Application.Context, firstPendingIntentId, intent, PendingIntentFlags.OneShot);
PendingIntent pendingIntent =
PendingIntent.GetActivity(Application.Context, pendingIntentId, playPauseIntent, PendingIntentFlags.OneShot);
// Build the notification:
var builder = new NotificationCompat.Builder(Application.Context, CHANNEL_ID)
.SetStyle(new Android.Support.V4.Media.App.NotificationCompat.MediaStyle()
.SetMediaSession(mSession.SessionToken)
.SetShowActionsInCompactView(0, 1))
.SetVisibility(NotificationCompat.VisibilityPublic)
.SetContentIntent(firstPendingIntent) // Start up this activity when the user clicks the intent.
.SetDeleteIntent(MediaButtonReceiver.BuildMediaButtonPendingIntent(Application.Context, PlaybackState.ActionStop))
.SetSmallIcon(Resource.Drawable.app_icon) // This is the icon to display
.AddAction(Resource.Drawable.ic_media_pause_dark, "Pause", pendingIntent)
.AddAction(Resource.Drawable.ic_media_play_dark, "Next", pendingIntent)
.SetContentText(GlobalResources.playerPodcast.EpisodeTitle)
.SetContentTitle(GlobalResources.playerPodcast.ChannelTitle);
// Finally, publish the notification:
var notificationManager = NotificationManagerCompat.From(Application.Context);
notificationManager.Notify(NOTIFICATION_ID, builder.Build());
};
player.EpisodeProgressChanged += (object sender, EventArgs e) =>
{
};
}
И тогда моя деятельность выглядит так
[Activity]
public class SecondActivity : Activity
{
DabPlayer player = GlobalResources.playerPodcast;
EpisodeViewModel Episode;
DroidDabNativePlayer droid = new DroidDabNativePlayer();
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
if (player.IsReady)
{
if (player.IsPlaying)
{
player.Pause();
droid.Init(player, true);
}
else
{
player.Play();
droid.Init(player, true);
}
}
else
{
if (player.Load(Episode.Episode))
{
player.Play();
droid.Init(player, true);
}
else
{
//DisplayAlert("Episode Unavailable", "The episode you are attempting to play is currently unavailable. Please try again later.", "OK");
}
}
Finish();
}
}
Я также хочу просто перейти к одной кнопке и заставить ее переключаться между показом паузы и воспроизведения в зависимости от состояния, если кто-то хочет указать мне правильное направление для этого.
Любая помощь приветствуется! Благодарю вас!