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

C# 如何在CarPlay Xamarin.iOS中制作NowPlay屏幕?如何在MPPlayableContentManager中设置nowPlayingIdentifiers属性?,c#,xamarin,xamarin.forms,xamarin.ios,carplay,C#,Xamarin,Xamarin.forms,Xamarin.ios,Carplay,我已经在Xamarin.Forms中创建了音频应用程序,为了播放音频,我使用了MediaManager插件 现在我想让它与CarPlay兼容 CarPlay音频应用程序由MPPlayableContentManager控制。你 实现MPPlayableContentDelegate和 MPPlayableContentDatasource协议,以便与CarPlay连接。 用户界面由CarPlay控制——您所需要做的就是向它提供数据 对于选项卡+表格(数据源)和响应可播放项(委托) 我已经为音频应

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

现在我想让它与CarPlay兼容

CarPlay音频应用程序由MPPlayableContentManager控制。你 实现MPPlayableContentDelegate和 MPPlayableContentDatasource协议,以便与CarPlay连接。 用户界面由CarPlay控制——您所需要做的就是向它提供数据 对于选项卡+表格(数据源)和响应可播放项(委托)

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

  • 现在无法在CarPlay模拟器中播放屏幕。
  • 如何在MPContentItem中设置艺术品?
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, NSIndexPath indexPath, 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);
    }
}
所以,问题是我现在应该如何回应可玩物品


有人知道我遗漏了什么或者我必须纠正哪些错误吗?任何帮助都将不胜感激,谢谢。

要获得“现在播放”屏幕,您必须设置两件事,务必做到。

  • 远程指挥中心
  • var commandCenter=MPRemoteCommandCenter.Shared;
    commandCenter.PlayCommand.Enabled=true;
    commandCenter.StopCommand.Enabled=true;
    
  • MPPlayableContentManager.NowPlayingIdentifiers
  • var urlId=“11111”;
    字符串[]标识符=新字符串[1];
    标识符[0]=urlId;
    contentManager=MPPlayableContentManager.Shared;
    contentManager.NowPlayingIdentifiers=标识符;
    
    您是否添加了断点并检查您编写的代码是否已执行?调试不同的标题和副标题是从哪里来的?是的,我已经检查了代码,它被执行了。当它执行时,我得到了我想要发送的正确名称@JackHua-MSFT1。)“不同”有多远?请提供您期望和看到的详细信息。2.)您是否创建了音频会话并在某个点将其设置为活动状态?在模拟器中,NowPlaying屏幕不可靠,您需要舞蹈才能使其工作(至少在iOS13之前,不知道14是否再次改变了情况…@DrMickeyLauer如何创建音频会话?你是说NowPlaying在模拟器上不起作用?@Divyesh_08我不是说它不起作用,但它可能需要一些奇怪的操作来调用
    beginReceivingRemoteControlEvents()
    endReceivingRemoteControlEvents()
    。至于音频会话,您需要获取当前的
    AVAudioSession.sharedInstance()
    ,然后至少设置一个配置(
    setConfiguration()
    )并将其设置为活动状态(
    setActive()
    ),否则iOS将不会传递任何音频。
    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;
        }
    }