I'm working on an app written in Xamarin.iOS/Forms that plays music. We want to be able to control the playback of the song/songlist from within the app or from the now playing info center. Everything is working as expected except I have not been able to programmatically control the state of the play/pause button in the info center when the user presses play/pause from within the app. My understanding from several swift examples is that you should be able to set the state by setting MPNowPlayingInfo.PlaybackRate to 1.0d for playing or 0.0d for paused. Here is some code from the app.
When a new song starts I add the song using the following code:
_nowPlayingInfo = new MPNowPlayingInfo();
_nowPlayingInfo.Artist = songDetails.Artist;
_nowPlayingInfo.Title = songDetails.Title;
_nowPlayingInfo.PlaybackDuration = durationSeconds;
_nowPlayingInfo.PlaybackRate = 0.0d;
var data = NSData.FromUrl(new NSUrl(songDetails.Album2xUrl));
var image = new UIImage(data:data);
_nowPlayingInfo.Artwork = new MPMediaItemArtwork(image);
MPNowPlayingInfoCenter.DefaultCenter.NowPlaying = _nowPlayingInfo;
Everything works as expected except the initial state of the play/pause button shows the pause button and with PlaybackRate set to 0.0d I expected it to show the play button.
As the song plays I update the using the following code:
_nowPlayingInfo.ElapsedPlaybackTime = elapsedSeconds;
MPNowPlayingInfoCenter.DefaultCenter.NowPlaying = _nowPlayingInfo;
This works as expected.
When the play/pause state updates from within the app I try to update the play/pause button in info center using the following code: which I have found in numerous swift examples but this has not affect on the play/pause button:
if (isPlaying)
_nowPlayingInfo.PlaybackRate = 1.0d;
else
_nowPlayingInfo.PlaybackRate = 0.0d;
MPNowPlayingInfoCenter.DefaultCenter.NowPlaying = _nowPlayingInfo;
Console.WriteLine($"---- {MPNowPlayingInfoCenter.DefaultCenter.NowPlaying.PlaybackRate}");
I can see from the console that PlaybackRate is set to the expected value but changing the value has not effect on the play/pause state in info center.
Has anyone else had this issue in Xamarin.iOS? Is there a different way to update the state of the play/pause button?
I'm using Xamarin.iOS Version: 16.1.1.27, Xcode 14.2, and testing on an iOS 16.3.1 device.