我遇到了iOS控制中心音樂控件的問題在iOS 11更新之前,Play Pause按鈕已啓用並正常工作,如預期的那樣。iOS控制中心音樂控制在iOS 11更新中停止工作(remoteControlReceivedWithEvent不稱爲iOS 11)
但是,在iOS 11中它停止工作。研究之後,我發現,在IOS 11 remoteControlReceivedWithEvent永遠不會被調用,而是,在舊的IOS版本例如iOS 9它被通常稱爲
設置我的事件上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;
}
}
}
也我訂閱了遠程事件在另一個類控制播放/暫停按鈕
- (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
remoteControlReceivedWithEvent not Called in appDelegate