Search code examples
iosxamarin.iosxamarin.formscontrol-center

Xamarin forms receive control centre event while playing songs in background


I am using plugin in xamarin to play songs on iOS and Android device which is working out perfectly.My problem is that I am unable to find way to listen to next and previous button press in control centre.

enter image description here

I have read blog for reference,but it doesn't seem to provide enough info to achieve solution.


Solution

  • You can find the related information in its README.md.

    • If you want to enable RemoteControl features, you will have to override UIApplication.RemoteControlReceived(UIEvent) and forward the event to the MediaManagerImplementation.MediaRemoteControl.RemoteControlReceived(UIEvent) method. See the sample application for more details.

    Follow these steps:

    1. Create a SampleApplication class inherit from UIApplication To implement RemoteControlReceived(UIEvent) as its sample code, like this:

      using Foundation;
      using Plugin.MediaManager;
      using UIKit;
      
      namespace MediaSample.iOS
      {
          [Register("SampleApplication")]
          public class SampleApplication: UIApplication
          {
              private MediaManagerImplementation MediaManager => (MediaManagerImplementation) CrossMediaManager.Current;
      
              public override void RemoteControlReceived(UIEvent uiEvent)
              {               
                 MediaManager.MediaRemoteControl.RemoteControlReceived(uiEvent);
              }
          }
      }
      

      MediaManager.MediaRemoteControl.RemoteControlReceived(uiEvent) helps you to handle the next/previous/pause/play events. To see how it works, you can refer here.

    2. Don't foget to add the SampleApplication to UIApplication.Main() in Main.cs like this:

       UIApplication.Main(args, "SampleApplication", "AppDelegate");