这可能是一个非常晚的答案,但我注意到,没有多少Q /经过关于音频播放和遥控器,所以我希望我的回答可以帮助其他有相同问题的人:
我目前使用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
。我不确定他们是否必须执行,如果有人知道它,让我们知道。
如何使用MPMoviePlayerController播放声音? – 2011-05-09 16:46:59