Xamarin 表单在后台播放歌曲时接收控制中心事件

Xamarin forms receive control centre event while playing songs in background

我在 xamarin 中使用 plugin 在 iOS 和 Android 设备上播放歌曲 perfectly.My 问题是我无法找到方法在控制中心聆听下一个和上一个按钮的按下。

我已阅读 blog 作为参考,但它似乎没有提供足够的信息来实现解决方案。

您可以在其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.

按照以下步骤操作:

  1. 创建一个 SampleApplication class 继承自 UIApplication 实现 RemoteControlReceived(UIEvent) 作为其 sample code,像这样:

    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) 帮助您处理 next/previous/pause/play 事件。具体如何操作可以参考here.

  2. 不要忘记将 SampleApplication 添加到 Main.cs 中的 UIApplication.Main(),如下所示:

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