Reacting to ControlCenter events when using AVPlayer for HLS audio playback

て烟熏妆下的殇ゞ 提交于 2019-12-08 08:37:12

问题


I am looking for a way to handle the play/pause events from the iOS ControlCenter when playing audio (HLS) using AVPlayer.

I have it all working, but it is based on "named" notifications which are not exposed in the header files.

Is there an "official" way to do this?

Currently the following code works:

- (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:


Solution

The solution to this was to watch the UIEvents entering the app and create my own notifications from here.

The relevant event type is:

UIEventTypeRemoteControl

The relevant event subtypes are:

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


来源:https://stackoverflow.com/questions/22457659/reacting-to-controlcenter-events-when-using-avplayer-for-hls-audio-playback

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!