我有app可以在后台播放音乐但由于某些原因,某些用户在应用停止在后台播放音乐时会遇到问题。我在自己的iPhone上几次注意到这个bug,但我不明白为什么会这样? 我正在使用AVAudioFoundation.framework
首先,我通过继承UIWindow
:
@interface MelomanWindow : UIWindow
@property (nonatomic, assign) UIResponder *responder;
- (void)remoteControlReceivedWithEvent:(UIEvent *)event;
@end
--------------------------------------
@implementation MelomanWindow
@synthesize responder = _responder;
- (void)remoteControlReceivedWithEvent:(UIEvent *)receivedEvent {
[self.responder remoteControlReceivedWithEvent:receivedEvent];
}
@end
在AplicationDelegate.m
:
[application beginReceivingRemoteControlEvents];
self.window.responder = [[ApplicationAudioService sharedInstance] playerViewController];
之后在我的ViewController
中执行此操作:
- (id)init {
...
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
[audioSession setActive:YES error:nil];
audioSession.delegate = self;
...
}
杀死应用后,Bug消失了。 当出现此错误时,远程控制器事件也无法正常工作。 当出现此错误时,状态栏中也会显示“播放”图标。