這可能是一個非常晚的答案,但我注意到,沒有多少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