如何使用 INPlayMediaIntent 处理 Siri 套件以在 Xamarin.iOS 应用程序中播放 Podcast 剧集?

How to handle Siri kit with INPlayMediaIntent to play Podcast episode in Xamarin.iOS app?

对于 Siri,我已按照此处提到的所有步骤进行操作: https://docs.microsoft.com/en-us/xamarin/ios/platform/sirikit/implementing-sirikit?tabs=macos

还关注:https://developer.apple.com/documentation/sirikit/media/managing_audio_with_sirikit

IntentHandler.cs:

[Register("IntentHandler")]
public class IntentHandler : INExtension, IINPlayMediaIntentHandling, IINAddMediaIntentHandling
{
    protected IntentHandler(IntPtr handle) : base(handle)
    { 
    }

    public override NSObject GetHandler(INIntent intent)
    {
        return this;
    }

    //INPlayMediaIntent

    public void resolveMediaItems(INPlayMediaIntent intent, Action<INPlayMediaMediaItemResolutionResult> completion)
    {
        var mediaItemData = new INMediaItem(identifier: SessionService.LastPlayedEpisode.PodcastId, title: SessionService.LastPlayedEpisode.Title, type: INMediaItemType.PodcastEpisode, artwork: null);
        MessagingCenter.Send<object, string>(this, "PlayRecentEpisodeSiriEvent", "ResolveMediaItems");
        completion(INPlayMediaMediaItemResolutionResult.GetSuccess(mediaItemData));
    }

    public void HandlePlayMedia(INPlayMediaIntent intent, Action<INPlayMediaIntentResponse> completion)
    {
        var userActivity = new NSUserActivity("INPlayMediaIntent");
        var response = new INPlayMediaIntentResponse(INPlayMediaIntentResponseCode.HandleInApp, userActivity);
        completion(response);
    }

    public void HandleAddMedia(INAddMediaIntent intent, Action<INAddMediaIntentResponse> completion)
    {
        var userActivity = new NSUserActivity("INAddMediaIntent");
        var response = new INAddMediaIntentResponse(INAddMediaIntentResponseCode.Success, userActivity);
        completion(response);
    }
}

现在,当我命令 Siri 点赞:“在我的应用程序名称中播放 The Daily”时,它会显示:

所以,我实际上并不知道 siri 将如何处理媒体内容以使用命令显示我的应用程序。任何人都知道:

  1. 如何在 Xamarin.iOS 中处理 Siri Media Intent?
  2. 如何使用 Xamarin.iOS 添加 Siri 支持?

UPDATE: 29 Oct, 2020 - How to play particular episode of Podcast from Playlist is siri?

我正在尝试播放 Podcast 播放列表的特定剧集,但无法播放。我已经给 siri 命令如下:Play the Overlap by audiochuck from CounterClock podcast in siridemo

here siridemo = my app's name, Overlap = episode name, counterclock = Podcast name.

当控制进入 appdelegate 中的 HandleIntent 方法时,我在 mediasearch.medianame 对象中只得到 CounterClock。那么用 Siri 可以做到这一点吗?

如何解决应用程序没有添加对 siri 错误的支持?

我找到了解决方案,就在这里。


1. IntentExtension 项目中的 Intent Handler:

First mistake I have done was I have used INPlayMediaIntentResponseCode.Success instead of HandleInApp, which I have corrected here. Why HandleInApp? Hold on :)

[Register("IntentHandler")]
public class IntentHandler : INExtension, IINPlayMediaIntentHandling
{
   protected IntentHandler(IntPtr handle) : base(handle) { }

   public override NSObject GetHandler(INIntent intent)
   {
      return this;
   }

   [Export("handlePlayMedia:completion:")]
   public void HandlePlayMedia(INPlayMediaIntent intent, Action<INPlayMediaIntentResponse> completion)
   {
      var response = new INPlayMediaIntentResponse(INPlayMediaIntentResponseCode.HandleInApp, null);
      completion(response);
   }
}


2。现在让我们在 AppDelegate 中处理它:

As we have passed HandleInApp as a response in Intent handler's HandlePlayMedia() it will call HandleIntent() method in AppDelegate of your main iOS project. Where the magic will happen.

public async override void HandleIntent(UIApplication application, INIntent intent, Action<INIntentResponse> completionHandler)
{
   var playMediaIntent = intent as INPlayMediaIntent;
   
   // The command you will give to siri will come here in MediaSearch object 
   var mediaName = playMediaIntent?.MediaSearch?.MediaName;

   // Check if the Searched Podcast and episode is there in your Podcasts list or 
   // not and handle it accordingly
   var response = new INPlayMediaIntentResponse(INPlayMediaIntentResponseCode.Failure, userActivity);;
   
   if(mediaName.Exist(Playlist))
   {
       response = new INPlayMediaIntentResponse(INPlayMediaIntentResponseCode.ContinueInApp, null);
   }
   else
   {
       response = new INPlayMediaIntentResponse(INPlayMediaIntentResponseCode.FailureUnknownMediaType, null);
   }
   completionHandler(response);
}


Give search command to siri like this - 

Play [EpisodeName] of [PodcastName] in your app name

For Example: `Play Conclusion in Selling Girls of America in siridemo app`

输出:

Now in MediaSearch.MediaName you will get "Conclusion in Selling Girls of America" as search string.

编码愉快!