Перейти к основному содержимому

AI-запись звука

Запускайте и останавливайте AI-запись звука для заданного RecordingScene.

Предварительные условия

  • Устройство подключено и готово к работе.
  • Устройство поддерживает 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")
    }
}

Обработка ошибок

Храните активную сцену в приложении, чтобы передать её в запрос остановки. При ошибке обработчика сохраняйте осторожное состояние интерфейса до следующего обновления состояния записи от устройства.