处理遥控快门
通过 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
- Objective-C
/// 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?
)
}/// The protocol for device remote shutter API.
@protocol AIBudsDeviceRemoteShutterAPI <AIBudsDeviceAPI>
/// 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.
- (void)syncRemoteShutterStateToDevice:(enum AIBudsRemoteShutterState)state
completion:(AIBudsCompletionHandler _Nullable)completion;
@end请参阅 API Reference 中的 syncRemoteShutterStateToDevice。
代理回调
- Swift
- Objective-C
/// Callback when remote shutter event received
/// - Parameters:
/// - device: the device
/// - event: the remote shutter event
optional func device(
_ device: DeviceConvertible,
didReceiveRemoteShutterEvent event: RemoteShutterEvent
)/// Callback when remote shutter event received
/// - Parameters:
/// - device: the device
/// - event: the remote shutter event
- (void)device:(id<AIBudsDeviceConvertible> _Nonnull)device
didReceiveRemoteShutterEvent:(enum AIBudsRemoteShutterEvent)event;使用示例
示例将 App 相机的具体实现交给本地方法,使 SDK 事件流程保持清晰。
- Swift
- Objective-C
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
}
}- (void)device:(id<AIBudsDeviceConvertible>)device
didReceiveRemoteShutterEvent:(AIBudsRemoteShutterEvent)event {
id<AIBudsDeviceRemoteShutterAPI> remoteShutter = (id<AIBudsDeviceRemoteShutterAPI>)device;
if (![remoteShutter conformsToProtocol:@protocol(AIBudsDeviceRemoteShutterAPI)])
return;
switch (event) {
case AIBudsRemoteShutterEventEnter:
[self prepareAppCameraWithCompletion:^(BOOL ready) {
AIBudsRemoteShutterState state = ready ? AIBudsRemoteShutterStateEnteredPhotoMode
: AIBudsRemoteShutterStateExitedOrUnableToEnter;
[remoteShutter syncRemoteShutterStateToDevice:state completion:nil];
}];
break;
case AIBudsRemoteShutterEventCapture:
[self captureAppPhotoWithCompletion:^(BOOL success) {
AIBudsRemoteShutterState state = success ? AIBudsRemoteShutterStatePhotoSuccess
: AIBudsRemoteShutterStatePhotoFailed;
[remoteShutter syncRemoteShutterStateToDevice:state completion:nil];
}];
break;
case AIBudsRemoteShutterEventExit:
[self stopAppCamera];
[remoteShutter syncRemoteShutterStateToDevice:AIBudsRemoteShutterStateExitedOrUnableToEnter
completion:nil];
break;
default:
break;
}
}错误处理
因权限被拒绝、存储空间不可用或 App 相机无法拍摄而失败时,上报 .photoFailed。App 无法进入拍照模式或已经退出时,上报 .exitedOrUnableToEnter。