使用AVPlayer进行HLS音频播放时,对ControlCenter事件做出反应

时间:2014-03-17 14:56:37

标签: iphone ipad ios7 avplayer control-center

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

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

是否有#34;官员"这样做的方法?

目前以下代码有效:

- (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;
                                                      }
                                                  }];
}

1 个答案:

答案 0 :(得分:1)

<强>解决方案

解决方案是观察UIEvents进入应用程序并从此处创建自己的通知。

相关事件类型为:

UIEventTypeRemoteControl

相关事件子类型是:

UIEventSubtypeRemoteControlPlay                 = 100,
UIEventSubtypeRemoteControlPause                = 101,
UIEventSubtypeRemoteControlStop                 = 102,
UIEventSubtypeRemoteControlTogglePlayPause      = 103,
UIEventSubtypeRemoteControlNextTrack            = 104,
UIEventSubtypeRemoteControlPreviousTrack        = 105,
UIEventSubtypeRemoteControlBeginSeekingBackward = 106,
UIEventSubtypeRemoteControlEndSeekingBackward   = 107,
UIEventSubtypeRemoteControlBeginSeekingForward  = 108,
UIEventSubtypeRemoteControlEndSeekingForward    = 109,