我正在寻找一种在使用 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;
}
}];
}