使用 AVFoundation 时,如何监听 Co​​ntrol Center Screen 和 Notification Center Screen activity

When using AVFoundation, how to listen for Control Center Screen and Notification Center Screen activity

我使用 AVFoundation 进行视频录制。当应用程序进入后台时,我停止捕获会话,当它进入前台时,我重新启动捕获会话,一切正常。我还使用 callKit 来监听传入的 phone 来电,这也很好用:

NotificationCenter.default.addObserver(self, selector: #selector(appWillEnterBackground), name: UIApplication.willResignActiveNotification, object: nil)
        
NotificationCenter.default.addObserver(self, selector: #selector(appWillEnterForeground), name: UIApplication.willEnterForegroundNotification, object: nil)

@objc func appWillEnterBackground() {

    // if recording stop recording, stop timer, etc ...
    captureSession.stopRunning()
    previewLayer = nil
}

@objc func appWillEnterForeground() {

   if !captureSession.isRunning {
        captureSession.startRunning()
        initialize preview layer
   }
}

问题是当 vc 与相机处于活动状态时(录制或不录制)以及当我从底部滑动以调出控制中心屏幕或从顶部滑动以关闭通知中心时调用屏幕 UIApplication.willResignActiveNotification 并停止捕获会话。当我删除其中任何一个屏幕时 UIApplication.willEnterForegroundNotification 不会被调用并且捕获会话不再是 运行.

我想做的是,当这些屏幕中的任何一个出现时,我只需使用布尔值来防止捕获会话停止

var haveControlScreensSurfaced = false // toggle this true/false depending when the control screens enter and leave

 @objc func appWillEnterBackground() {

    if view.window != nil && haveControlScreensSurfaced { return }

    // if recording stop recording, stop timer, etc ...
    captureSession.stopRunning()
    previewLayer = nil
}

如何专门监听 Control Center ScreenNotification Center Screen activity 以便我可以将我的 haveControlScreensSurfaced 布尔值切换为 true/false?

这个way works good for an avplayer

长话短说,因为我需要一个捕获会话,所以我使用了它。当滑动up/down通知中心屏幕或控制屏幕时,UIApplication.didEnterBackgroundNotification(应用程序进入后台)和UIApplication.willEnterForegroundNotification(应用程序即将进入前台)永远不会被调用.我只是将我的代码移到那里,问题就解决了:

NotificationCenter.default.addObserver(self, selector: #selector(didEnterBackground), name: UIApplication.didEnterBackgroundNotification, object: nil)
        
NotificationCenter.default.addObserver(self, selector: #selector(appWillEnterForeground), name: UIApplication.willEnterForegroundNotification, object: nil)

@objc func didEnterBackground() {

    // stop capture session
}

@objc func appWillEnterForeground() {

   // start capture session
}

以下是触发通知时发生的情况的细分:

按下主页按钮,将应用程序发送到后台:

1- `UIApplication.willResignActiveNotification` gets called first
2- `UIApplication.didEnterBackgroundNotification` gets called second // *** gets called when the HomeButton is pressed ***

正在打开应用备份:

1- `UIApplication.willEnterForegroundNotification` gets called first  // *** gets called when the opening the app back up ***
2- `UIApplication.didBecomeActiveNotification` gets called second

从顶部向下滑动通知中心屏幕:

1- `UIApplication.willResignActiveNotification` gets called first
2- `UIApplication.didBecomeActiveNotification` gets called second
3- if using the `.AVCaptureSessionWasInterrupted` the `.videoDeviceNotAvailableInBackground` gets called third
4- `UIApplication.willResignActiveNotification` gets called fourth

向后滑动通知中心屏幕:

1- `UIApplication.didBecomeActiveNotification` gets called alone
2- if using the `.AVCaptureSessionInterruptionEnded` it gets called second

从底部向上滑动控制屏幕:

1- `UIApplication.willResignActiveNotification` gets called alone

将控制屏幕向下滑动:

1- `UIApplication.didBecomeActiveNotification` gets called by alone