微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

MPRemoteCommandCenter播放/暂停命令无法正确更新

如何解决MPRemoteCommandCenter播放/暂停命令无法正确更新

我已经设置了播放/暂停按钮,并且为每个按钮都添加了目标事件,但是当我在模拟器中单击按钮时,它总是调用播放按钮的目标事件,并且按钮的图像从播放到暂停都没有正确更新,反之亦然。有人对此有想法吗?

注意:要播放/暂停音频,我使用了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;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?