我已经AVPlayer
通过MPNowPlayingInfoCenter
. 但我找不到如何像标准音乐应用程序那样添加 ±15 秒倒带按钮。所以问题是如何在锁定屏幕上添加这个按钮?
问问题
1499 次
1 回答
5
我现在正在使用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" fast-forwarding in this method.
}
}
}
这在我的应用程序中运行良好,但是如果您希望能够在所有视图控制器中接收远程控制事件,那么您应该在AppDelegate
.
于 2013-12-03T21:27:52.893 回答