AI 录音
为指定的 RecordingScene 启动和停止 AI 录音。
前置条件
- 设备已连接且就绪。
- 设备符合
DeviceAudioRecordingAPI。 - 停止录音时使用与启动时相同的场景。
API 参考
框架
AIBuds.xcframework
导入
- Swift
- Objective-C
import AIBuds
import AIBudsFoundation#import <AIBuds/AIBuds-Swift.h>
#import <AIBuds/AIBuds.h>协议
- Swift
- Objective-C
/// 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?
)
}/// The protocol for device API that supports audio recording.
@protocol AIBudsDeviceAudioRecordingAPI <AIBudsDeviceAPI>
/// 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.
- (void)startAIAudioRecordingWithScene:(enum AIBudsRecordingScene)scene
completion:(AIBudsCompletionHandler _Nullable)completion;
/// 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.
- (void)stopAIAudioRecordingWithScene:(enum AIBudsRecordingScene)scene
completion:(AIBudsCompletionHandler _Nullable)completion;
@end实例方法
| 方法 | 用途 |
|---|---|
startAIAudioRecording | 为场景启动 AI 录音。 |
stopAIAudioRecording | 停止该场景的 AI 录音。 |
录音场景
| Swift | Objective-C | 描述 |
|---|---|---|
.onSite | AIBudsRecordingSceneOnSite | 环境或现场录音。 |
.call | AIBudsRecordingSceneCall | 电话通话期间录音。 |
完成回调报告 success 和可选的 NSError。这些方法不返回转写文本或录音文件路径。
使用示例
- Swift
- Objective-C
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")
}
}#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;
}
AIBudsRecordingScene scene = AIBudsRecordingSceneOnSite;
[device startAIAudioRecordingWithScene:scene
completion:^(BOOL success, NSError *_Nullable error) {
if (!success) {
NSLog(@"AI recording failed to start: %@",
error.localizedDescription);
return;
}
NSLog(@"AI recording started");
}];
// Stop using the same scene.
[device stopAIAudioRecordingWithScene:scene
completion:^(BOOL success, NSError *_Nullable error) {
if (!success) {
NSLog(@"AI recording failed to stop: %@",
error.localizedDescription);
}
}];错误处理
在应用中记录活动场景,以便停止请求传入匹配的场景。回调失败时,在设备报告下一次录音状态更新前保持保守的 UI 状态。