这可能是一个很晚的答案,但正如我注意到的,关于音频播放和远程控制的 Q/As 并不多,所以我希望我的回答能帮助其他有同样问题的人:
我现在用的是AVAudioPlayer,但是- (void)remoteControlReceivedWithEvent:(UIEvent *)event这个遥控方式不能和你使用的播放器类型有关系。
要使锁定屏幕上的前进和后退按钮正常工作,请按照以下步骤操作:
在您的视图控制器的viewDidLoad 方法中添加以下代码:
//Make sure the system follows our playback status - to support the playback when the app enters the background mode.
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
然后添加这些方法:
viewDidAppear::(如果尚未实现)
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
//Once the view has loaded then we can register to begin recieving controls and we can become the first responder
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
}
viewWillDisappear:(如果尚未实现)
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
//End recieving events
[[UIApplication sharedApplication] endReceivingRemoteControlEvents];
[self resignFirstResponder];
}
还有:
//Make sure we can recieve remote control events
- (BOOL)canBecomeFirstResponder {
return YES;
}
- (void)remoteControlReceivedWithEvent:(UIEvent *)event {
//if it is a remote control event handle it correctly
if (event.type == UIEventTypeRemoteControl)
{
if (event.subtype == UIEventSubtypeRemoteControlPlay)
{
[self playAudio];
}
else if (event.subtype == UIEventSubtypeRemoteControlPause)
{
[self pauseAudio];
}
else if (event.subtype == UIEventSubtypeRemoteControlTogglePlayPause)
{
[self togglePlayPause];
}
else if (event.subtype == UIEventSubtypeRemoteControlBeginSeekingBackward)
{
[self rewindTheAudio]; //You must implement 15" rewinding in this method.
}
else if (event.subtype == UIEventSubtypeRemoteControlBeginSeekingForward)
{
[self fastForwardTheAudio]; //You must implement 15" fastforwarding in this method.
}
}
}
这在我的应用程序中运行良好,但是如果您希望能够在所有视图控制器中接收远程控制事件,那么您应该在 AppDelegate 中设置它。
注意! 这段代码目前运行良好,但我看到了另外两个子类型,称为UIEventSubtypeRemoteControlEndSeekingBackward 和UIEventSubtypeRemoteControlEndSeekingBackward。我不确定它们是否必须实施,如果有人知道,请告诉我们。