我在使用 avplayer 控制 iPhone 控件时遇到问题。
如果我输入函数
- (void)remoteControlReceivedWithEvent:(UIEvent *)event
在负责播放所调用函数的 View Controller 中,但前提是我要在当前 View Controller 中返回后台。
如果我要从其他 View Controller 进入后台,则该函数永远不会被调用。
这就是为什么我想把它放在应用程序委托(delegate)中。
我尝试了 Becomefirstresponse 并将该函数放入每个 View Controller 中,但它确实有帮助。
我也打电话
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
在
-(void)applicationWillResignActive:(UIApplication *)application
谢谢
最佳答案
我已将以下代码用于 iPhone 控制 -
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
用于获取监听 Remote 的注册。 完成后将其删除 -
[[UIApplication sharedApplication] endReceivingRemoteControlEvents];
[self resignFirstResponder];
使应用可以成为第一响应者-
- (BOOL)canBecomeFirstResponder {
return YES;
}
使用委托(delegate)方法来处理 iPhone 控制,比如双击 home 键时播放和暂停
- (void)remoteControlReceivedWithEvent:(UIEvent *)event {
//if it is a remote control event handle it correctly
if (event.type == UIEventTypeRemoteControl) {
if (event.subtype == UIEventSubtypeRemoteControlPlay) {
[audioPlayer play];
NSLog(@"play");
} else if (event.subtype == UIEventSubtypeRemoteControlPause) {
[audioPlayer stop];
NSLog(@"pause");
} else if (event.subtype == UIEventSubtypeRemoteControlTogglePlayPause) {
NSLog(@"toggle");
}
}
}
就我而言,我能够处理播放和暂停。如果有任何问题,请告知。
关于iphone - remoteControlReceivedWithEvent 未在 appDelegate 中调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11862317/