Ios 我如何知道用户在iPhone的播放控件上单击快进和快退按钮
我要落实以下几点,Ios 我如何知道用户在iPhone的播放控件上单击快进和快退按钮,ios,objective-c,avfoundation,mpmovieplayercontroller,playback,Ios,Objective C,Avfoundation,Mpmovieplayercontroller,Playback,我要落实以下几点, 应用程序正在后台运行音乐或视频(使用MPMoviePlayerController) 用户双击home(主页)按钮,进入显示播放控制的第一个屏幕(快退、播放或暂停、快进按钮) 用户单击快退或快进按钮。 然后应用程序播放上一个或下一个音乐或视频 对于第三步,我应该知道单击了哪个按钮。 (我自然知道,当前播放的项目已暂停、停止..使用mpmovieplayerplayerblaybackstatedidchangenotificationnotification) 我应该注册哪个
MPMoviePlayerController
)mpmovieplayerplayerblaybackstatedidchangenotification
notification)
我应该注册哪个通知?或者还有其他方法吗?我自己得到了答案 即使用UIApplication的beginReceivingRemoteControlEvents 在适当的位置(如ViewWillExample:)放置以下代码
[[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;
这可能是一个很晚的答案,但正如我所注意到的,关于音频播放和遥控的Q/a并不多,所以我希望我的答案能帮助其他有同样问题的人: 目前我正在使用
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];
然后添加以下方法:
视图显示:
:(如果尚未实现)
视图将消失:
(如果尚未实现)
以及:
这在我的应用程序中运行良好,但是如果您希望能够在所有视图控制器中接收远程控制事件,则应在AppDelegate
中进行设置
注意这段代码目前运行正常,但我看到另外两个子类型,分别是
uieventsubsubmitRemoteControlEndSeekingBackward
和uieventsubmitRemoteControlEndSeekingBackward
。我不确定是否必须实现它们,如果有人知道,请告诉我们。在运行iPhone 4.1设备的模拟器中,这似乎不起作用?是这种情况还是我做错了什么?我似乎有完全相同的设置,但就是无法获取任何事件。请检查您是否成为第一响应者。并检查其他类是否在将接收称为becomeFirstResponder的事件的类之后调用becomeFirstResponder。无论捕获远程控制事件是否有效,似乎都有不同的体验。Alones,你能为你的解决方案提供一个独立的示例吗?我得到这个是为了通过iPhone耳机控件响应按钮点击,但不是屏幕上的按钮。是否有人有幸捕捉到屏幕上的“下一步/上一步”按钮?如何使用MPMoviePlayerController播放声音?您是否在iOS 8中进行过测试?因为似乎不起作用@Neeku@NorthBlast不,我在一年多前就在做这个,但很快需要在iOS 8上更新和测试…@NorthBlast,顺便说一句,你可能认为它不起作用,但实际上你需要长时间点击(点击并保持一段时间)使它倒带。它没有苹果自己的播客应用程序那么好,但由于对图书馆的访问有限,这已经足够令人满意了。
- (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];
}
- (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.
}
}
}