Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用beginReceivingRemoteControlEvents_Ios_Objective C_Avaudiosession - Fatal编程技术网

Ios 使用beginReceivingRemoteControlEvents

Ios 使用beginReceivingRemoteControlEvents,ios,objective-c,avaudiosession,Ios,Objective C,Avaudiosession,我看到人们使用:[[UIApplication sharedApplication]开始接收RemoteControlEvents]在管理音频会话时处理远程控制事件 我的问题: 此代码是否只能在UIViewController类或AppDelegate类中使用?因为我看到互联网上的每个人都在这两个班中的一个班上使用它。我可以在不是UIViewController或AppDelegate子类的类中使用它吗?对于那些只想在特定状态下处理远程控制事件的应用,例如,准备播放媒体,与出现在设置或注册屏幕上

我看到人们使用:
[[UIApplication sharedApplication]开始接收RemoteControlEvents]在管理音频会话时处理远程控制事件

我的问题:


此代码是否只能在
UIViewController
类或
AppDelegate
类中使用?因为我看到互联网上的每个人都在这两个班中的一个班上使用它。我可以在不是UIViewController或AppDelegate子类的类中使用它吗?

对于那些只想在特定状态下处理远程控制事件的应用,例如,准备播放媒体,与出现在设置或注册屏幕上不同,从视图控制器调用
beginReceivingRemoteControlEvents
具有一定的意义。其他应用可能希望完全抑制远程控制行为(因为它们接管音频会话,不希望用户触发背景音频并破坏会话数据)

然而,没有什么可以阻止你将这种行为分解到应用程序的另一个领域,特别是如果它是共享的
beginReceivingRemoteControlEvents
UIApplication
上的一种方法,因此只要您能够获得
[UIApplication sharedApplication]
的句柄,就可以标记应用程序以开始/结束远程控制事件处理

但是,值得注意的是,该方法已被弃用:

在iOS 7.1及更高版本中,使用共享MPRemoteCommandCenter对象注册远程控制事件。使用共享命令中心对象时,不需要调用此方法。[]


在这一点上,我的远程控制事件处理经验已经有几年了,但我记得在接收RemoteControlEvents时,有一些处理
的非直觉行为。快速浏览一下,它看起来像是处理远程控制事件的更好的API。如果您的用例不需要iOS 7.0支持,您应该调查该API。

[[UIApplication sharedApplication]开始接收RemoteControlEvents]
可以在应用程序中的任何位置使用,例如在开始播放音频之前,或在启动时在
应用程序:didFinishLaunchingWithOptions:
委托方法中使用

对于希望在整个应用程序生命周期(和后台)中使用控件进行后台音频播放的应用程序,我建议在AppDelegate中调用
beginReceivingRemoteControlEvents
。这样,您就可以在应用程序生命周期的任何时候明确地接收远程控制事件

提到帕尔帕蒂姆
MPRemoteCommandCenter
。作为一个围绕后台音频播放(想想收音机)构建了完整应用程序的人,我强烈推荐这种方法,而不是旧的
UIEvent
回调。我在中详细介绍了这种方法,但要点是使用
MPRemoteCommandCenter.sharedCommandCenter()
启用或禁用锁屏和控制中心上的播放控制,以及提供选择器:

let commandCenter = MPRemoteCommandCenter.sharedCommandCenter()

commandCenter.previousTrackCommand.enabled = true;
commandCenter.previousTrackCommand.addTarget(self, action: #selector(previousTrack))

commandCenter.nextTrackCommand.enabled = false
commandCenter.nextTrackCommand.addTarget(self, action: #selector(nextTrack))
请注意,如果要明确禁用控件,除了在命令上设置
enabled=false
,还必须为操作提供[dummy]选择器

要阐明您关于成为第一响应者的观点,请执行以下操作:

从UIResponder继承的任何对象,如
UIViewController
UIApplicationLegate
都可以成为第一个响应者,只要该对象实现了处理远程控制事件所需的方法
remoteControlReceivedWithEvent:
。例如,我可以让AppDelegate成为第一响应者,并在其中处理所有远程控制事件,并发送通知,通知
UIViewController
侦听暂停播放机或跳到下一个曲目。您可以让一个
UIViewController
成为第一响应者(持有玩家参考的控制器),并直接控制玩家。该体系结构非常开放,这实际上取决于您如何构建应用程序。你没有提供任何代码,所以我不知道你的播放器设置是什么样子的

您的
UIResponder
处理代码可能如下所示:

- (void)remoteControlReceivedWithEvent:(UIEvent *)event {
    if (event.type == UIEventTypeRemoteControl) {
        switch (event.subtype) {
            case UIEventSubtypeRemoteControlTogglePlayPause:
                // Pause or play action
                break;
            case UIEventSubtypeRemoteControlNextTrack:
                // Next track action
                break;
            case UIEventSubtypeRemoteControlPreviousTrack:
                // Previous track action
                break;
            case UIEventSubtypeRemoteControlStop:
                // Stop action
                break;
            default:
                // catch all action
                break;
        }
    }
}
同样,是否将其放置在控制器或AppDelegate中取决于您

我可以在不是UIViewController或AppDelegate子类的类中使用它吗


是的,您可以在继承自
UIResponder
的任何类中实现这一点。人们通常使用AppDelegate或视图控制器是为了方便。

谢谢,但人们说我需要像这样调用
[self becomeFirstResponder]
,这表明它只能在UIResponder的子类中使用。这就是为什么我要问这个问题,我想在一个与UI或UIResponder无关的类中使用它。@Leem.fin实现
remoteControlReceivedWithEvent:
的类应该调用
becomeFirstResponder
。我将编辑我的答案。还要注意,AppDelegate是
UIResponder
的一个子类。您有使用MPRemoteCommandCenter的示例代码吗?thanks@JaredChu对不起,我没有直接使用它的经验。