我已启用播放/暂停按钮,并为每个按钮添加了目标事件,但是当我在模拟器中单击按钮时,它总是调用播放按钮的目标事件和按钮的图像不能正确地从播放更新到暂停,反之亦然。有人对此有想法吗?
注意:要播放/暂停音频,我使用了 Plugin.MediaManager.Forms nuget(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;
}