跳到主要内容

处理遥控快门

通过 DeviceDelegate 接收遥控快门事件,执行对应的 App 相机操作,并将结果同步给设备。

前提条件

  • 设备已连接,并遵循 DeviceRemoteShutterAPI
  • App 已配置 NSCameraUsageDescription 并请求相机授权。
  • App 自行持有 AVCaptureSession 并实现照片拍摄。

使用 AI 辅助实现

使用 AI 开发

让 AI 帮助实现此工作流

使用官方“实现 AIBuds 遥控快门”技能,根据你的 App 完成实现。

请阅读并遵循 https://docs-aibuds.github.io/zh-Hans/skills/implement-aibuds-remote-shutter,使用该技能在当前 iOS 项目中完成“实现 AIBuds 遥控快门”,并验证结果。
查看官方技能

API 参考

协议

Swift
/// The protocol for device remote shutter API.
protocol DeviceRemoteShutterAPI: DeviceAPI {
    /// Syncs the app’s real-time remote-shutter response state to the device after the device triggers remote shutter.
    /// - Parameters:
    ///   - state: The real-time remote-shutter response state to sync to the device.
    ///   - completion: A closure that is called when the operation completes.
    ///     - success: `true` if the operation was successful; otherwise `false`.
    ///     - error: An `NSError` object that describes the error that occurred, or `nil` if the operation was successful.
    func syncRemoteShutterStateToDevice(
        _ state: RemoteShutterState,
        completion: AIBudsCompletionHandler?
    )
}

请参阅 API Reference 中的 syncRemoteShutterStateToDevice

代理回调

Swift
/// Callback when remote shutter event received
/// - Parameters:
///   - device: the device
///   - event: the remote shutter event
optional func device(
    _ device: DeviceConvertible,
    didReceiveRemoteShutterEvent event: RemoteShutterEvent
)

使用示例

示例将 App 相机的具体实现交给本地方法,使 SDK 事件流程保持清晰。

Swift
func device(
    _ device: DeviceConvertible,
    didReceiveRemoteShutterEvent event: RemoteShutterEvent
) {
    guard let remoteShutter = device as? DeviceRemoteShutterAPI else { return }

    switch event {
    case .enter:
        prepareAppCamera { ready in
            let state: RemoteShutterState =
                ready
                ? .enteredPhotoMode
                : .exitedOrUnableToEnter
            remoteShutter.syncRemoteShutterStateToDevice(state, completion: nil)
        }
    case .capture:
        captureAppPhoto { success in
            remoteShutter.syncRemoteShutterStateToDevice(
                success ? .photoSuccess : .photoFailed,
                completion: nil
            )
        }
    case .exit:
        stopAppCamera()
        remoteShutter.syncRemoteShutterStateToDevice(
            .exitedOrUnableToEnter,
            completion: nil
        )
    case .unknown:
        break
    }
}

错误处理

因权限被拒绝、存储空间不可用或 App 相机无法拍摄而失败时,上报 .photoFailed。App 无法进入拍照模式或已经退出时,上报 .exitedOrUnableToEnter