在我的应用程序,我想,让用户来控制后台音频播放。 我的.plist设置背景模式,并在BG戏就像我wanted.But我不能得到触摸控制按钮任何回应。
我设置了AudioSession
这样
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance]setActive:YES error:nil];
然后,在viewContoller在我的球员放在我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
为什么能这样呢?
这里的解决方案就是解决我的问题上的iOS4进入后台播放音频