我遇到iOS控制中心音乐控件的问题在iOS 11更新之前,Play Pause按钮已启用并正常工作,正如预期的那样 .
但是,在iOS 11中它停止了工作 . 经过研究,我发现在IOS 11中,remoteControlReceivedWithEvent永远不会被调用,但是,在iOS 9这样的旧iOS版本中,它被正常调用
我在 AppDelegate.m
设置了我的活动
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Enable background audio listening
// https://developer.apple.com/library/ios/qa/qa1668/_index.html
NSError *error = nil;
if (![[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&error]) {
NSLog(@"%@", error);
}
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
}
- (void)remoteControlReceivedWithEvent:(UIEvent *)receivedEvent {
if (receivedEvent.type == UIEventTypeRemoteControl) {
switch (receivedEvent.subtype) {
case UIEventSubtypeRemoteControlPlay:
[[NSNotificationCenter defaultCenter] postNotificationName:kCYCAppDelegatePlayPauseNotificationName object:nil];
break;
case UIEventSubtypeRemoteControlPause:
[[NSNotificationCenter defaultCenter] postNotificationName:kCYCAppDelegatePlayPauseNotificationName object:nil];
break;
case UIEventSubtypeRemoteControlTogglePlayPause:
[[NSNotificationCenter defaultCenter] postNotificationName:kCYCAppDelegatePlayPauseNotificationName object:nil];
break;
default:
break;
}
}
}
我还订阅另一个 class 的远程事件来控制播放/暂停按钮
- (void)subscribeToRemoteControlEvents {
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.1")) {
// Disables the forward/backward buttons and only shows the play button.
// You can't just enable the command, you must subscribe for this to activate, so
// the subscription in this case doesn't do anything.
[MPRemoteCommandCenter sharedCommandCenter].togglePlayPauseCommand.enabled = YES;
[[MPRemoteCommandCenter sharedCommandCenter].togglePlayPauseCommand addTarget:self action:@selector(ignore_removeCommandCenterFired)];
}
[[NSNotificationCenter defaultCenter] addObserver:self forName:kCYCAppDelegatePlayPauseNotificationName object:nil queue:nil usingBlock:^(NSNotification *note, CYCCastManager *observer) {
if (observer.isCastPlaying) {
[observer pause];
}
else {
[observer play:NO];
}
}];
}
- (void)unsubscribeFromRemoteControlEvents {
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.1")) {
[[MPRemoteCommandCenter sharedCommandCenter].togglePlayPauseCommand removeTarget:self action:@selector(ignore_removeCommandCenterFired)];
}
}
我想知道为什么不再工作了我在文档中检查了API的变化,但我没有看到变化
注意:我检查以下链接没有运气
iOS - UIEventTypeRemoteControl events not received
https://forums.developer.apple.com/thread/84204
Unable to receive remoteControlReceivedWithEvent - objective c - ios
remoteControlReceivedWithEvent in AVAudio is not being called
2 回答
最后,我通过使用remoteCommandCenter和播放和暂停按钮而不是tooglePlayPauseCommand来解决问题
刚刚胡安回答修改 .