在我的应用程序中,我想让用户控制后台音频播放。我在 .plist 中设置了背景模式,并在背景中设置了播放模式,就像我想要的那样。但是我无法通过触摸控制按钮得到任何响应。
我设置了AudioSession
像这样
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance]setActive:YES error:nil];
然后,在我的播放器所在的 viewController 中我beginReceivingRemoteControlEvents
像这样
if ([[UIApplication sharedApplication] respondsToSelector:@selector(beginReceivingRemoteControlEvents)]){
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL];
[self becomeFirstResponder];
NSLog(@"Responds!");
}
它打印Responds!
但问题是这个方法永远不会被调用
- (void)remoteControlReceivedWithEvent:(UIEvent *)event
{
NSLog(@"Where is my event?");
if(event.type == UIEventTypeRemoteControl)
{
switch (event.subtype) {
case UIEventSubtypeRemoteControlTogglePlayPause:
NSLog(@"Pause");
[self playWords:playButton];
break;
case UIEventSubtypeRemoteControlNextTrack:
NSLog(@"Next");
[self next];
break;
case UIEventSubtypeRemoteControlPreviousTrack:
NSLog(@"Prev");
[self prev];
break;
}
}
我什至尝试写一个类别UIApplication
让它成为第一响应者,但这并没有帮助
@implementation UIApplication (RemoteEvents)
-(BOOL)canBecomeFirstResponder
{
return YES;
}
@end
为什么会发生这种情况?
SOLUTION这就是解决我的问题的方法iOS4进入后台播放音频 https://stackoverflow.com/questions/3161635/entering-background-on-ios4-to-play-audio/3161899#3161899