在iOS 7.0设备上调用remoteControlReceivedWithEvent但不在iOS 8.0上调用

我有一个在后台播放音频的应用程序。 我正在尝试修复一个错误,其中音频控制(播放/暂停),在主屏幕(等),不在iOS 8.0+上工作,但在iOS 7.0上工作FINE。 我一直在努力弄清楚问题是什么,并且一直空洞。 任何想法将不胜感激。 这就是我所拥有的。

在项目设置中:我已确保将UIBackgroundModes设置为audio

在AppDelegate.h中:我有一个AVAudioSession* session;的成员AVAudioSession* session; 以及AVPlayer *audioPlayer;

在AppDelegate.m中:

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ self.session = [AVAudioSession sharedInstance]; NSError* error = NULL; [self.session setCategory:AVAudioSessionCategoryPlayback error:&error]; [self.session setActive:YES error:&error]; if (error) { NSLog(@"AVAudioSession error: %@", [error localizedDescription]); } 

在AudioPlayerViewController.m中

 - (void)viewDidLoad { //Get the Audio NSURL *url = [NSURL URLWithString:self.audioUrl]; AVAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil]; //Setup the player self.playerItem = [AVPlayerItem playerItemWithAsset:asset]; appDelegate.audioPlayer = [AVPlayer playerWithPlayerItem:self.playerItem]; //Setup the "Now Playing" NSMutableDictionary *mediaInfo = [[NSMutableDictionary alloc]init]; [mediaInfo setObject:self.title forKey:MPMediaItemPropertyTitle]; [mediaInfo setObject:self.artist forKey:MPMediaItemPropertyArtist]; [mediaInfo setObject:self.album forKey:MPMediaItemPropertyAlbumTitle]; [mediaInfo setObject:[NSNumber numberWithDouble:duration ] forKey:MPMediaItemPropertyPlaybackDuration]; [[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:mediaInfo]; 

}

 // Process remote control events - (void) remoteControlReceivedWithEvent:(UIEvent *)event { NSLog(@"AudioPlayerViewController ... remoteControlReceivedWithEvent top ....subtype: %d", event.subtype); if (event.type == UIEventTypeRemoteControl) { switch (event.subtype) { case UIEventSubtypeRemoteControlTogglePlayPause: [self togglePlayPause]; break; case UIEventSubtypeRemoteControlPause: [self doPause]; break; case UIEventSubtypeRemoteControlStop: [self doPause]; break; case UIEventSubtypeRemoteControlPlay: [self doPlay]; break; case UIEventSubtypeRemoteControlPreviousTrack: [self nextOrPrevTrack]; break; case UIEventSubtypeRemoteControlNextTrack: [self nextOrPrevTrack]; break; default: break; } } 

}

 -(void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; [self becomeFirstResponder]; 

}

 - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [[UIApplication sharedApplication] endReceivingRemoteControlEvents]; [self resignFirstResponder]; 

}

 - (BOOL) canBecomeFirstResponder { return YES; 

}

终于找到了我遇到的问题。 最终,似乎主屏幕上远程控制的事件从未进入我的应用程序,直到我的视图控制器。 我最终UIWindowUIWindow子类,以便我可以看到哪些事件正在通过链。 由于UIWindow是一个UIResponder我还在子类中添加了- (void)remoteControlReceivedWithEvent:(UIEvent *)event 。 然后在makeKeyAndVisible中添加了:

 [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; [self becomeFirstResponder]; 

我启动了调试器,从未调用过-(void)makeKeyAndVisible ! 然后我在我的app委托中搜索了window成员变量和[window makeKeyAndVisible]; 线路无处可寻! 我把它添加回来(因为它应该在那里)并且presto事件正在路由到魔法等正确的位置。 为什么这适用于某些版本的iOS而不是其他版本而且没有任何其他明显的问题超出了我的范围。

希望这有助于将来的人。

在你的ViewController中添加

 override func viewWillAppear(animated: Bool) { super.viewWillAppear(animated) UIApplication.sharedApplication().beginReceivingRemoteControlEvents() self.becomeFirstResponder() } override func remoteControlReceivedWithEvent(event: UIEvent) { // your stuff } 

在AppDelegate中添加

 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { var error: NSError? AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, error: &error) AVAudioSession.sharedInstance().setActive(true, error: &error) } 

SWIFT 3

 UIApplication.shared.beginReceivingRemoteControlEvents() self.becomeFirstResponder() do { try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback) try AVAudioSession.sharedInstance().setActive(true) } catch { print("hmmm...") }