1

我有一个可以在后台播放音乐的应用程序,但由于某种原因,当应用程序停止在后台播放音乐时,一些用户会遇到问题。我在自己的 iPhone 上多次注意到这个错误,但我不明白为什么会发生这种情况?我正在使用 AVAudioFoundation.framework

首先,我通过子类化为 remoteEvents 注册自己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 消失。出现此错误时,遥控器事件也不起作用。当此错误出现时,状态栏中也未显示“播放”图标。

4

0 回答 0