跳到主要内容

AI 录音

为指定的 RecordingScene 启动和停止 AI 录音。

前置条件

  • 设备已连接且就绪。
  • 设备符合 DeviceAudioRecordingAPI
  • 停止录音时使用与启动时相同的场景。

API 参考

框架

AIBuds.xcframework

导入

Swift
import AIBuds
import AIBudsFoundation

协议

Swift
/// The protocol for device API that supports audio recording.
protocol DeviceAudioRecordingAPI: DeviceAPI {
    /// Starts an AI audio recording for the specified scene.
    /// - Parameters:
    ///   - scene: The recording scenario context.
    ///   - 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 startAIAudioRecording(
        _ scene: RecordingScene,
        completion: AIBudsCompletionHandler?
    )

    /// Stops the AI audio recording for the specified scene.
    /// - Parameters:
    ///   - scene: The recording scenario context.
    ///   - 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 stopAIAudioRecording(
        _ scene: RecordingScene,
        completion: AIBudsCompletionHandler?
    )
}

实例方法

方法用途
startAIAudioRecording为场景启动 AI 录音。
stopAIAudioRecording停止该场景的 AI 录音。

录音场景

SwiftObjective-C描述
.onSiteAIBudsRecordingSceneOnSite环境或现场录音。
.callAIBudsRecordingSceneCall电话通话期间录音。

完成回调报告 success 和可选的 NSError。这些方法不返回转写文本或录音文件路径。

使用示例

Swift
import AIBuds

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

let scene: RecordingScene = .onSite
device.startAIAudioRecording(scene) { success, error in
    guard success else {
        print("AI recording failed to start: \(error?.localizedDescription ?? "Unknown error")")
        return
    }
    print("AI recording started")
}

// Stop using the same scene.
device.stopAIAudioRecording(scene) { success, error in
    if !success {
        print(error?.localizedDescription ?? "AI recording failed to stop")
    }
}

错误处理

在应用中记录活动场景,以便停止请求传入匹配的场景。回调失败时,在设备报告下一次录音状态更新前保持保守的 UI 状态。