标准录音
请求已连接设备开始或停止标准音频录音会话。
前置条件
- 设备已连接且就绪。
- 设备符合
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 可用时向用户报告;公开协议未说明各状态码的含义。