使用 AVCaptureEventInteraction 进行音量按钮快门捕捉

问题描述 投票:0回答:1

我已收到正式通知,iOS 17.2 包含新的 API,可正式支持相机应用程序中通过音量按钮驱动进行照片拍摄。

API 称为

AVCaptureEventInteraction
链接

那么如果我使用 AVCam 示例代码,我将如何附加此交互?

    if #available(iOS 17.2, *) {
       let interaction = AVCaptureEventInteraction { event in
             print ("AVCaptureEventInteraction Fired")
       }
    }

API 不包含任何详细信息。

ios swift xcode volume avkit
1个回答
0
投票

您需要将

AVCaptureEventInteraction
集成到
AVCaptureSession
或相关组件中。
API 的用法可能如下所示:

if #available(iOS 17.2, *) {
    let interaction = AVCaptureEventInteraction { event in
        // Handle the volume button press event here
        // e.g., Trigger photo capture
        print("AVCaptureEventInteraction Fired")
    }
    // Add the interaction to your capture session or related component
    // That might depend on further details from the API documentation
    captureSession.addInteraction(interaction)
}

但是,由于

AVCaptureEventInteraction
API 文档并不详细,您可能需要尝试在
AVCaptureSession
设置中的何处添加此交互。可能的位置是在初始化会话之后和开始之前。
我在 GitHub 上尝试了快速搜索...但什么也没找到(!)

您可以尝试在设置会话之后但启动会话之前,在

AVCaptureEventInteraction
示例的
configureSession
方法中添加
AVCam
初始化。
AVCaptureEventInteraction
的闭包内,实现按下音量按钮时拍摄照片的逻辑。将交互添加到
AVCaptureSession

func configureSession() {
    // existing setup code for AVCaptureSession 

    if #available(iOS 17.2, *) {
        let interaction = AVCaptureEventInteraction { event in
            // Implement the logic to capture a photo
            print("AVCaptureEventInteraction Fired")
            // Example: self.capturePhoto()
        }
        captureSession.addInteraction(interaction)
    }

    // rest of the session configuration 
}

self.capturePhoto()
替换为 AVCam 中用于拍摄照片的实际方法。

© www.soinside.com 2019 - 2024. All rights reserved.