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

740 Views Asked by At

I have followed all steps mentioned here for Siri: https://learn.microsoft.com/en-us/xamarin/ios/platform/sirikit/implementing-sirikit?tabs=macos

Also followed: 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);
    }
}

Now, when I give command to Siri to like: "Play The Daily in myappname" it show this:

So, I am not getting actually how siri will handle Media content to show my app with command. Anyone know:

  1. How to handle Siri Media Intent in Xamarin.iOS ?
  2. How to add Siri Support with Xamarin.iOS?

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

I am trying to play particular episode of Podcast playlist but it's not working. I have given command to siri like: Play the Overlap by audiochuck from CounterClock podcast in siridemo

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

I am getting only CounterClock in mediasearch.medianame object when control comes to HandleIntent method in appdelegate. So it is possible to achieve this with Siri?

How to fix app hasn't added support for that with siri error?

enter image description here

1

There are 1 best solutions below

0
On BEST ANSWER

I have got the solution and it's here.


1. Intent Handler in your IntentExtension project:

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. Now let's Handle it in 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`

Output:

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

Happy Coding!