是否可以使用Xamarin工具开发CarPlay应用

时间:2019-05-11 19:33:01

标签: c# visual-studio xamarin

正如标题所述,我正在尝试为我的汽车构建一个应用程序。我只在Xamarin文档中看到brief mention的CarPlay。

那么可以使用Xamarin和Visual Studio工具开发CarPlay应用吗?

P.S。我进行了更多研究,虽然您可以为CarPlay开发应用程序,但Apple仅在撰写本文时才允许导航和流式应用程序。因此,对于我想做的事情来说,这是一个完全的初学者。

2 个答案:

答案 0 :(得分:2)

是可能的。 我做了一个blog post和一个关于GitHub的例子。


简而言之:

在我们的iOS项目中,我们使用CarIntegrationBridge.Init();在AppDelegate.cs中初始化委托。

代表注册如下:

public class CarIntegrationBridge : ICarIntegrationBridge
{
  public static void Init()
  {
    PlayableContentDelegate playableContentDelegate = new PlayableContentDelegate();
    MPPlayableContentManager.Shared.Delegate = playableContentDelegate;
    PlayableContentDataSource playableContentDataSource = new PlayableContentDataSource();
    MPPlayableContentManager.Shared.DataSource = playableContentDataSource;
  }
}

现在,我们定义数据源。在我的示例中,我添加了带有名称和网址的广播电台。我们必须定义菜单项的数量以及菜单项的显示方式(名称,图标等):

internal class PlayableContentDataSource : MPPlayableContentDataSource
{
  public static List<Station> Stations = new List<Station>
  {
    new Station{Name = "Rainbow radio", Url = "https://stream.rockantenne.de/rockantenne/stream/mp3"},
    new Station{Name = "Unicorn radio", Url = "http://play.rockantenne.de/heavy-metal.m3u"}
  };
  
  public override MPContentItem ContentItem(NSIndexPath indexPath)
  {
    var station = Stations[indexPath.Section];
    var item = new MPContentItem(station.Url);
    item.Title = station.Name;
    item.Playable = true;
    item.StreamingContent = true;
    var artWork = GetImageFromUrl("station.png");
    if (artWork != null)
    {
      item.Artwork = artWork;
    }
    return item;
  }
  public override nint NumberOfChildItems(NSIndexPath indexPath)
  {
    if (indexPath.GetIndexes().Length == 0)
    {
      return Stations.Count;
    }
    throw new NotImplementedException();
  }
  private MPMediaItemArtwork GetImageFromUrl(string imagePath)
  {
    MPMediaItemArtwork result = null;
    try
    {
      using (var nsUrl = new NSUrl(imagePath))
      {
        using (var data = NSData.FromUrl(nsUrl))
        {
          var image = UIImage.LoadFromData(data);
          result = new MPMediaItemArtwork(image);
        }
      }
    }
    catch
    {
      UIImage image = UIImage.FromBundle(imagePath);
      if (image != null)
      {
        result = new MPMediaItemArtwork(image);
      }
    }
    return result;
  }
}

现在,如果要粘贴内容,我们必须决定要做什么。 模拟器的行为不同于真实设备。因此,我破解了调用NowPlayingScene的解决方案。

internal class PlayableContentDelegate : MPPlayableContentDelegate
{
  public override void InitiatePlaybackOfContentItem(
    MPPlayableContentManager contentManager, NSIndexPath indexPath, Action<NSError> completionHandler)
  {
    Execute(contentManager, indexPath);
    completionHandler?.Invoke(null);
  }
  private void Execute(MPPlayableContentManager contentManager, NSIndexPath indexPath)
  {
    DispatchQueue.MainQueue.DispatchAsync(async () => await ItemSelectedAsync(contentManager, indexPath));
  }
  private async Task ItemSelectedAsync(MPPlayableContentManager contentManager, NSIndexPath indexPath)
  {
    // Play
    var station = PlayableContentDataSource.Stations[indexPath.Section];
    await CrossMediaManager.Current.Play(station.Url);
    // Set playing identifier
    MPContentItem item = contentManager.DataSource.ContentItem(indexPath);
    contentManager.NowPlayingIdentifiers = new[] { item.Identifier };
    // Update on simulator
    if (DeviceInfo.DeviceType == DeviceType.Virtual)
    {
      InvokeOnMainThread(() =>
      {
        UIApplication.SharedApplication.EndReceivingRemoteControlEvents();
        UIApplication.SharedApplication.BeginReceivingRemoteControlEvents();
      });
    }
  }
}

要重新加载数据(例如,如果您更改电台),则必须调用此名称:

public void ReloadStations()
{
  MPPlayableContentManager.Shared?.ReloadData();
}

答案 1 :(得分:1)

也许这个https://forums.xamarin.com/discussion/144790/android-auto-ios-car-play回答了问题?

  

Car Play似乎是苹果方面的私有框架。而且没有有关XCode的相关文档。

     

对于Android Auto,请转到github,搜索项目Xamarin_Android_Auto_Test

相关问题