我有一个具有MainViewController,PlaylistViewController和PlayerViewController的应用程序。

当您返回主屏幕时,在后台播放音频都可以正常工作。

PlayerViewController在其viewDidAppear中调用registerForRemoteNotifications,以捕获来自控制中心和耳机控件的通知。当此控制器在前台时,会收到来自控制中心/耳机的通知,暂停/播放正常。

但是,如果在播放音频时返回PlayListViewController或MainViewController,然后返回主屏幕(音频仍在按原样播放),但是显然不会再将来自控制中心的远程通知发送到PlayerViewController,因为更长的时间。

为此,我是否需要在AppDelegate与PlayerViewController中创建静态AVAudioPlayer对象,并将registerForRemoteNotifications与PlayerViewController中的逻辑一起移至AppDelegate,以处理来自控制中心和耳机设备的暂停/播放并访问AppDelegates静态对象我的PlayerViewController中的对象?

最佳答案

整个远程通知逻辑应该位于应用程序委托类中,因为这是在应用程序生命周期中唯一可靠存在的类。

由于可以从任何地方使用访问应用程序委托

let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate


您可以创建对其他类的引用,也可以实现委托方法以能够正确处理通知。

10-08 01:35