メインコンテンツまでスキップ

リモートシャッターの処理

DeviceDelegate からリモートシャッターイベントを受信し、対応するアプリ側カメラ操作を実行して、結果をデバイスへ同期します。

前提条件

  • デバイスが接続済みで、DeviceRemoteShutterAPI に準拠していること。
  • アプリに NSCameraUsageDescription が設定され、カメラの使用許可を要求していること。
  • アプリが AVCaptureSession と写真撮影実装を管理していること。

AI を活用して実装

AI で実装

AI でこのワークフローを実装

公式の「AIBuds リモートシャッターの実装」スキルを使い、アプリに合わせて実装します。

https://docs-aibuds.github.io/ja/skills/implement-aibuds-remote-shutter を読み、その指示に従ってください。このスキルで「AIBuds リモートシャッターの実装」をこの iOS プロジェクトに実装し、検証してください。
公式スキルを見る

API Reference

プロトコル

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
)

使用例

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
    }
}

エラー処理

権限拒否、空き容量不足、アプリ側カメラの撮影失敗時は .photoFailed を通知します。写真モードへ移行できない場合や、すでに終了している場合は .exitedOrUnableToEnter を通知します。