跳到主要内容

标准录音

请求已连接设备开始或停止标准音频录音会话。

前置条件

API 参考

框架

AIBuds.xcframework

导入

Swift
import AIBuds

协议

这些请求由 DeviceAudioRecordingAPI 定义。

Swift
/// The protocol for device API that supports audio recording.
protocol DeviceAudioRecordingAPI: DeviceAPI {
    /// Requests the device to start an audio recording session.
    /// - Parameters:
    ///   - completion: A closure that is called when the operation completes.
    ///     - success: `true` if the operation was successful; otherwise `false`.
    ///     - statusCode: The status code returned by the device. `nil` if the operation failed.
    ///     - error: An `NSError` object that describes the error that occurred, or `nil` if the operation was successful.
    func requestStartAudioRecording(_ completion: AIBudsStatusCodeCompletionHandler?)

    /// Requests the device to stop the current audio recording session.
    /// - Parameters:
    ///   - completion: A closure that is called when the operation completes.
    ///     - success: `true` if the operation was successful; otherwise `false`.
    ///     - statusCode: The status code returned by the device. `nil` if the operation failed.
    ///     - error: An `NSError` object that describes the error that occurred, or `nil` if the operation was successful.
    func requestStopAudioRecording(_ completion: AIBudsStatusCodeCompletionHandler?)
}

实例方法

方法用途
requestStartAudioRecording请求设备开始录音。
requestStopAudioRecording请求设备停止录音。

完成回调值

名称类型描述
successBool / BOOL请求是否成功。
statusCodeNSNumber?设备返回的状态码;操作失败时为 nil
errorNSError?失败详情;成功时为 nil

这些方法不返回录音文件路径,结果是设备命令状态。

使用示例

Swift
import AIBuds

guard let device = device as? DeviceAudioRecordingAPI else {
    print("Device does not support audio recording")
    return
}

device.requestStartAudioRecording { success, statusCode, error in
    guard success else {
        print("Start failed: \(error?.localizedDescription ?? "Unknown error")")
        return
    }
    print("Recording started, status: \(statusCode?.stringValue ?? "Unavailable")")
}

// Call this from the UI action that stops the active recording.
device.requestStopAudioRecording { success, statusCode, error in
    guard success else {
        print("Stop failed: \(error?.localizedDescription ?? "Unknown error")")
        return
    }
    print("Recording stopped, status: \(statusCode?.stringValue ?? "Unavailable")")
}

错误处理

success 作为主要结果。保留返回的 statusCode 用于设备特定处理,并在 error 可用时向用户报告;公开协议未说明各状态码的含义。