1

我正在寻找一种在使用 AVPlayer 播放音频 (HLS) 时处理来自 iOS ControlCenter 的播放/暂停事件的方法。

我已经完成了所有工作,但它基于未在头文件中公开的“命名”通知。

有没有“官方”的方式来做到这一点?

目前以下代码有效:

- (void) removeControlCenterNotifications
{
    [[UIApplication sharedApplication] endReceivingRemoteControlEvents];
}

- (void) addControlCenterNotifications
{
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

    __weak MyClass     *pWeakSelf   = self;
    __weak MoviePlayer *pWeakPlayer = player_;

    [[NSNotificationCenter defaultCenter] addObserverForName:@"UIApplicationSimpleRemoteActionNotification"
                                                      object:nil
                                                       queue:NULL
                                                  usingBlock:^(NSNotification *notification)
                                                  {
                                                      if(pWeakSelf == nil) return;

                                                      NSNumber *type = notification.userInfo[@"UIApplicationSimpleRemoteActionType"];

                                                      switch ([type intValue]) {
                                                          case 6: [pWeakPlayer play]; break;
                                                          case 7: [pWeakPlayer pause]; break;
                                                      }
                                                  }];
}
4

1 回答 1

1

解决方案

解决这个问题的方法是观察 UIEvents 进入应用程序并从这里创建我自己的通知。

相关事件类型为:

UIEventTypeRemoteControl

相关的事件子类型是:

UIEventSubtypeRemoteControlPlay                 = 100,
UIEventSubtypeRemoteControlPause                = 101,
UIEventSubtypeRemoteControlStop                 = 102,
UIEventSubtypeRemoteControlTogglePlayPause      = 103,
UIEventSubtypeRemoteControlNextTrack            = 104,
UIEventSubtypeRemoteControlPreviousTrack        = 105,
UIEventSubtypeRemoteControlBeginSeekingBackward = 106,
UIEventSubtypeRemoteControlEndSeekingBackward   = 107,
UIEventSubtypeRemoteControlBeginSeekingForward  = 108,
UIEventSubtypeRemoteControlEndSeekingForward    = 109,
于 2014-03-18T04:33:46.810 回答