Обычная запись
Запросите запуск или остановку обычной записи звука на подключённом устройстве.
Предварительные условия
- Устройство подключено и готово к работе.
- Устройство поддерживает
DeviceAudioRecordingAPI. - На устройстве достаточно места для записи.
Справочник API
Фреймворк
AIBuds.xcframework
Импорт
- Swift
- Objective-C
import AIBuds#import <AIBuds/AIBuds-Swift.h>
#import <AIBuds/AIBuds.h>Протокол
Запросы определены протоколом DeviceAudioRecordingAPI.
- Swift
- Objective-C
/// 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?)
}/// The protocol for device API that supports audio recording.
@protocol AIBudsDeviceAudioRecordingAPI <AIBudsDeviceAPI>
/// 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.
- (void)requestStartAudioRecordingWithCompletion:
(AIBudsStatusCodeCompletionHandler _Nullable)completion;
/// 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.
- (void)requestStopAudioRecordingWithCompletion:
(AIBudsStatusCodeCompletionHandler _Nullable)completion;
@endМетоды экземпляра
| Метод | Назначение |
|---|---|
requestStartAudioRecording | Запросить запуск записи на устройстве. |
requestStopAudioRecording | Запросить остановку записи на устройстве. |
Значения завершения
| Имя | Тип | Описание |
|---|---|---|
success | Bool / BOOL | Успешно ли выполнен запрос. |
statusCode | NSNumber? | Код состояния устройства или nil, если операция не удалась. |
error | NSError? | Сведения об ошибке или nil при успехе. |
Методы не возвращают путь к файлу записи. Результатом является состояние команды устройства.
Примеры использования
- Swift
- Objective-C
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")")
}#import <AIBuds/AIBuds-Swift.h>
#import <AIBuds/AIBuds.h>
id<AIBudsDeviceAudioRecordingAPI> device = (id<AIBudsDeviceAudioRecordingAPI>)self.device;
if (![device conformsToProtocol:@protocol(AIBudsDeviceAudioRecordingAPI)]) {
NSLog(@"Device does not support audio recording");
return;
}
[device requestStartAudioRecordingWithCompletion:^(
BOOL success, NSNumber *_Nullable statusCode, NSError *_Nullable error) {
if (!success) {
NSLog(@"Start failed: %@", error.localizedDescription);
return;
}
NSLog(@"Recording started, status: %@", statusCode);
}];
// Call this from the UI action that stops the active recording.
[device requestStopAudioRecordingWithCompletion:^(
BOOL success, NSNumber *_Nullable statusCode, NSError *_Nullable error) {
if (!success) {
NSLog(@"Stop failed: %@", error.localizedDescription);
return;
}
NSLog(@"Recording stopped, status: %@", statusCode);
}];Обработка ошибок
Используйте success как основной результат. Сохраняйте statusCode для обработки особенностей устройства и показывайте error, если он есть; открытый протокол не описывает значения отдельных кодов состояния.