MPRemoteCommandCenter play/pause command not updating properly

291 views Asked by At

I have set play/pause button enabled and also added target event to each but when ever I click button in simulator it always calls play button's target event and button's image not properly updating from play to pause and vice versa. Any one have idea regards this?

Note: To play/pause audio I have used Plugin.MediaManager.Forms nuget (version 0.9.7)

[Export("playableContentManager:initiatePlaybackOfContentItemAtIndexPath:completionHandler:")]
public override void InitiatePlaybackOfContentItem(MPPlayableContentManager contentManager, NSIndexPath indexPath, Action<NSError> completionHandler)
{
    DispatchQueue.MainQueue.DispatchAsync(() =>
    {
        UIApplication.SharedApplication.EndReceivingRemoteControlEvents();
        UIApplication.SharedApplication.BeginReceivingRemoteControlEvents();

        var song = CarPlaylist[indexPath.Section].episodes[indexPath.Row];
        var NowPlayingInfoCenter = MPNowPlayingInfoCenter.DefaultCenter;

        MPNowPlayingInfo playingInfo = new MPNowPlayingInfo();
        playingInfo.Title = song.Title;
        playingInfo.Artist = song.Editor;
        playingInfo.PlaybackDuration = song.Duration.TotalSeconds; //provide time in seconds
        playingInfo.MediaType = MPNowPlayingInfoMediaType.Audio;
        playingInfo.Artwork = new MPMediaItemArtwork(image: ExtractArtWork.UIImageFromUrl(song.ArtWork));
        playingInfo.AssetUrl = new NSUrl(song.FileUrl.AbsolutePath);
        playingInfo.PlaybackRate = song.IsPlaying ? 1.0 : 0.0;
        NowPlayingInfoCenter.NowPlaying = playingInfo;

        var currentPlayItemId = episode.PodcastId.ToString();
        string[] identifier = new string[1];
        identifier[0] = currentPlayItemId;

        contentManager = MPPlayableContentManager.Shared;
        contentManager.NowPlayingIdentifiers = identifier;
        contentManager.DataSource = new AppDelegateDataSource(CarPlaylist);

        var commandCenter = MPRemoteCommandCenter.Shared;
        commandCenter.PlayCommand.Enabled = true;
        commandCenter.PauseCommand.Enabled = true;
        commandCenter.PlayCommand.AddTarget(PlayButton);
        commandCenter.PauseCommand.AddTarget(PauseButton);

        completionHandler(null);

        UIApplication.SharedApplication.EndReceivingRemoteControlEvents();
        UIApplication.SharedApplication.BeginReceivingRemoteControlEvents();
    });
}

public MPRemoteCommandHandlerStatus PlayButton(MPRemoteCommandEvent commandEvent)
{
    //Logic to update app UI to play mode and play audio
    return MPRemoteCommandHandlerStatus.Success;
}

public MPRemoteCommandHandlerStatus PauseButton(MPRemoteCommandEvent commandEvent)
{
    //Logic to update app UI to pause mode and pause audio
    return MPRemoteCommandHandlerStatus.Success;
}
0

There are 0 answers