0

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;
}
Divyesh
  • 2,085
  • 20
  • 35
  • Hi, you could have a try with a physical device to check that whether it can work. – Junior Jiang Oct 06 '20 at 03:23
  • @JuniorJiang-MSFT Yes, but actually my CarPlay Entitlement process is still in progress, so without getting that I can not test it in real device. – Divyesh Oct 06 '20 at 04:27

0 Answers0