我已经实现了我AVPlayer
在iPhone锁屏上的表示MPNowPlayingInfoCenter
.但我无法找到如何在标准音乐应用程序中添加±15秒倒带按钮.所以问题是如何在锁定屏幕上添加此按钮?
我现在正在使用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
.