[英]Reacting to ControlCenter events when using AVPlayer for HLS audio playback
I am looking for a way to handle the play/pause events from the iOS ControlCenter when playing audio (HLS) using AVPlayer. 我正在寻找一种使用AVPlayer播放音频(HLS)时从iOS ControlCenter处理播放/暂停事件的方法。
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;
}
}];
}
Solution 解
The solution to this was to watch the UIEvents entering the app and create my own notifications from here. 解决方案是观看UIEvents进入应用程序并从此处创建我自己的通知。
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,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.