如何在 CarPlay Xamarin.iOS 中制作 NowPlay 屏幕?以及如何在 MPPlayableContentManager 中设置 nowPlayingIdentifiers 属性? [英] How to make NowPlay screen in CarPlay Xamarin.iOS? and How to set nowPlayingIdentifiers property in MPPlayableContentManager?

查看:37
本文介绍了如何在 CarPlay Xamarin.iOS 中制作 NowPlay 屏幕?以及如何在 MPPlayableContentManager 中设置 nowPlayingIdentifiers 属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have created Audio App in Xamarin.Forms, for playing audio I have used MediaManager plugin.

现在我想让它与 CarPlay 兼容.

Now I want to make it compatible with CarPlay.

CarPlay 音频应用程序由 MPPlayableContentManager 控制.你需要实现 MPPlayableContentDelegate 和MPPlayableContentDatasource 协议,以便与 CarPlay 连接.用户界面由 CarPlay 控制 - 您需要做的就是为其提供数据用于选项卡+表格(数据源)并响应可玩项目(委托).

CarPlay audio apps are controlled by the MPPlayableContentManager. You are required to implement the MPPlayableContentDelegate and MPPlayableContentDatasource protocol in order to connect with CarPlay. The UI is controlled by CarPlay - all you need to do is feed it data for tabs+tables (datasource) and respond to playable items (delegate).

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

I have used all required CarPlay api for audio app but, the problem is:

  • 现在无法在 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, 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);
    }
}

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;
    }
}

那么,问题是我现在应该如何响应可玩项目?

So, the question is How should I respond to playable items now?

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

Anyone know what I'm missing or which mistake I have to correct? Any help would be appreciated, thanks.

推荐答案

要获得 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;

这篇关于如何在 CarPlay Xamarin.iOS 中制作 NowPlay 屏幕?以及如何在 MPPlayableContentManager 中设置 nowPlayingIdentifiers 属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆