使用AVPlayer进行HLS音频播放时,对ControlCenter事件做出反应 [英] Reacting to ControlCenter events when using AVPlayer for HLS audio playback
本文介绍了使用AVPlayer进行HLS音频播放时,对ControlCenter事件做出反应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在寻找一种方法来处理使用AVPlayer播放音频(HLS)时来自iOS ControlCenter的播放/暂停事件。
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?
目前以下代码有效:
- (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;
}
}];
}
推荐答案
解决方案
解决方法是观察UIEvents进入应用程序并从此处创建我自己的通知。
The solution to this was to watch the UIEvents entering the app and create my own notifications from here.
相关的事件类型是:
UIEventTypeRemoteControl
相关的事件子类型是:
UIEventSubtypeRemoteControlPlay = 100,
UIEventSubtypeRemoteControlPause = 101,
UIEventSubtypeRemoteControlStop = 102,
UIEventSubtypeRemoteControlTogglePlayPause = 103,
UIEventSubtypeRemoteControlNextTrack = 104,
UIEventSubtypeRemoteControlPreviousTrack = 105,
UIEventSubtypeRemoteControlBeginSeekingBackward = 106,
UIEventSubtypeRemoteControlEndSeekingBackward = 107,
UIEventSubtypeRemoteControlBeginSeekingForward = 108,
UIEventSubtypeRemoteControlEndSeekingForward = 109,
这篇关于使用AVPlayer进行HLS音频播放时,对ControlCenter事件做出反应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文