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

如何在CarPlay Xamarin.iOS中制作NowPlay屏幕?以及如何在MPPlayableContentManager中设置nowPlayingIdentifiers属性?

如何解决如何在CarPlay Xamarin.iOS中制作NowPlay屏幕?以及如何在MPPlayableContentManager中设置nowPlayingIdentifiers属性?

我已经在Xamarin.Forms中创建了音频应用程序,为了播放音频,我使用了MediaManager插件

现在,我想使其与CarPlay兼容。

CarPlay音频应用程序由MPPlayableContentManager控制。您 需要实现MPPlayableContentDelegate和 MPPlayableContentDatasource协议以便与CarPlay连接。 用户界面由CarPlay控制-您所需要做的就是输入数据 标签和表格(数据源)并响应可播放项(委托)。

我已经将所有必需的CarPlay api用于音频应用程序,但是问题是:

  • 现在无法在CarPlay模拟器中播放屏幕。
  • 如何在MPContentItem中设置ArtWork?

MPPlayableContentDelegate类

public class PlayableContentDelegate : MPPlayableContentDelegate
{
    public override void PlayableContentManager(MPPlayableContentManager contentManager,NSIndexPath indexPath,Action<NSError> completionHandler)
    {
        dispatchQueue.MainQueue.dispatchAsync(() =>
        {
            UIApplication.SharedApplication.BeginReceivingRemoteControlEvents();
            completionHandler(null);
            UIApplication.SharedApplication.EndReceivingRemoteControlEvents();

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

    [Export("playableContentManager:initiatePlaybackOfContentItemAtIndexPath:completionHandler:")]
    public override void InitiatePlaybackOfContentItem(MPPlayableContentManager contentManager,Action<NSError> completionHandler)
    {
        try
        {
            dispatchQueue.MainQueue.dispatchAsync(() =>
            {
                UIApplication.SharedApplication.BeginReceivingRemoteControlEvents();

                var itemToPlay = BaseSettingsService.CurrentPlayList[indexPath.Row];
                var NowPlayingInfoCenter = MPNowPlayingInfoCenter.DefaultCenter;

                MPNowPlayingInfo playingInfo = new MPNowPlayingInfo();
                playingInfo.Title = itemToPlay.Title;
                playingInfo.Artist = itemToPlay.Editor;
                playingInfo.AlbumTitle = "1989";
                playingInfo.Genre = "Pop";
                playingInfo.PlaybackDuration = 231;
                playingInfo.PlaybackRate = 22;
                playingInfo.PersistentID = (ulong)111111;
                playingInfo.PlaybackQueueIndex = 3;
                playingInfo.PlaybackQueueCount = BaseSettingsService.CurrentPlayList.Count;
                playingInfo.IsLiveStream = false;
                playingInfo.MediaType = MPNowPlayingInfoMediaType.Audio;
                NowPlayingInfoCenter.NowPlaying = playingInfo;

                var id = itemToPlay.podcastId.ToString();
                string[] s1 = new string[1];
                s1[0] = id;

                contentManager.NowPlayingIdentifiers = s1;

                completionHandler(null);

                UIApplication.SharedApplication.EndReceivingRemoteControlEvents();
            });
        }
        catch (Exception ex)
        {
            Debug.WriteLine(ex.Message);
        }
    }

    public override nuint RetainCount { get; }

    public override void ContextUpdated(MPPlayableContentManager contentManager,MPPlayableContentManagerContext context)
    {
        try
        {
            //base.ContextUpdated(contentManager,context);
        }
        catch (Exception ex)
        {
            Debug.WriteLine(ex.Message);
        }
    }

    public override NSDictionary GetDictionaryOfValuesFromKeys(Nsstring[] keys)
    {
        return base.GetDictionaryOfValuesFromKeys(keys);
    }
}

MPPlayableContentDataSource

public class AppDelegateDataSource : MPPlayableContentDataSource
{
    public override MPContentItem ContentItem(NSIndexPath indexPath)
    {
        if (indexPath.Length == 1)
        {
            var item = new MPContentItem("PlayList");
            item.Title = "PlayList";
            item.Subtitle = "Hello";
            item.Container = true;
            item.Playable = false;
            return item;
        }
        else
        {
            var play = CurrentPlayList[indexPath.Row];
            var item = new MPContentItem(play.podcastId);
            item.Title = play.Title;
            item.Subtitle = play.Editor;
            item.Playable = true;
            return item;
        }
    }

    public override nint NumberOfChildItems(NSIndexPath indexPath)
    {
        if (indexPath.GetIndexes().Length == 0)
            return 1;
        else
            return CurrentPlayList.Count;
    }
}

所以,问题是我现在应该如何应对可玩物品?

任何人都知道我缺少什么或必须纠正哪个错误?任何帮助将不胜感激,谢谢。

解决方法

要获得 NowPlaying 屏幕,您必须正确设置两件事。

  1. MPRemoteCommandCenter
var commandCenter = MPRemoteCommandCenter.Shared;
commandCenter.PlayCommand.Enabled = true;
commandCenter.StopCommand.Enabled = true;
  1. MPPlayableContentManager.NowPlayingIdentifiers
var urlId = "11111";
string[] identifier = new string[1];
identifier[0] = urlId;

contentManager = MPPlayableContentManager.Shared;
contentManager.NowPlayingIdentifiers = identifier;

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