我想实现以下内容,
MPMoviePlayerController
)。对于第3步,我应该知道单击了哪个按钮。
(我自然知道,当前播放的项目已暂停,停止使用MPMoviePlayerPlaybackStateDidChangeNotification
通知)。
我应该注册哪个通知?或者还有其他方法吗?
答案 0 :(得分:6)
我自己得到了答案。
那是使用UIApplication的beginReceivingRemoteControlEvents。
在适当的地方(如viewWillAppear :)输入以下代码
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
视图控制器应该实现以下方法返回YES
- (BOOL)canBecomeFirstResponder {
return YES;
}
然后您可以通过以下方法接收远程控制器事件。
- (void)remoteControlReceivedWithEvent:(UIEvent *)event {
if( event.type == UIEventTypeRemoteControl ) {
NSLog(@"sub type: %d", event.subtype);
}
}
event.subtype如下,
typedef enum {
// available in iPhone OS 3.0
UIEventSubtypeNone = 0,
// for UIEventTypeMotion, available in iPhone OS 3.0
UIEventSubtypeMotionShake = 1,
// for UIEventTypeRemoteControl, available in iPhone OS 4.0
UIEventSubtypeRemoteControlPlay = 100,
UIEventSubtypeRemoteControlPause = 101,
UIEventSubtypeRemoteControlStop = 102,
UIEventSubtypeRemoteControlTogglePlayPause = 103,
UIEventSubtypeRemoteControlNextTrack = 104,
UIEventSubtypeRemoteControlPreviousTrack = 105,
UIEventSubtypeRemoteControlBeginSeekingBackward = 106,
UIEventSubtypeRemoteControlEndSeekingBackward = 107,
UIEventSubtypeRemoteControlBeginSeekingForward = 108,
UIEventSubtypeRemoteControlEndSeekingForward = 109,
} UIEventSubtype;
答案 1 :(得分:1)
这可能是一个非常晚的答案,但正如我所注意到的,关于音频播放和遥控器的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
的子类型。我不确定是否必须实施,如果有人知道,请告诉我们。