リモートシャッターの処理
DeviceDelegate からリモートシャッターイベントを受信し、対応するアプリ側カメラ操作を実行して、結果をデバイスへ同期します。
前提条件
- デバイスが接続済みで、
DeviceRemoteShutterAPIに準拠していること。 - アプリに
NSCameraUsageDescriptionが設定され、カメラの使用許可を要求していること。 - アプリが
AVCaptureSessionと写真撮影実装を管理していること。
AI を活用して実装
AI で実装
AI でこのワークフローを実装
公式の「AIBuds リモートシャッターの実装」スキルを使い、アプリに合わせて実装します。
https://docs-aibuds.github.io/ja/skills/implement-aibuds-remote-shutter を読み、その指示に従ってください。このスキルで「AIBuds リモートシャッターの実装」をこの iOS プロジェクトに実装し、検証してください。API Reference
プロトコル
- 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;
@endAPI 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;使用例
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;
}
}エラー処理
権限拒否、空き容量不足、アプリ側カメラの撮影失敗時は .photoFailed を通知します。写真モードへ移行できない場合や、すでに終了している場合は .exitedOrUnableToEnter を通知します。