同声传译
启动长时间运行的语音同声传译会话,接收增量源文本、译文、可选 TTS 音频、事件和最终报告。
有关 SimultaneousInterpretationEventType 生命周期以及 onEvent 的处理方式,请参阅 AI 会话事件。
Animated workflow
同声传译会话生命周期
协调服务商启动、当前音频源、有序增量结果、中断处理和最终停止。
前提条件
AIBudsAISDK已初始化,并已选择和鉴权注册的服务商。- 服务商支持
SimultaneousInterpretationServiceAPI。 - 源语言和目标语言标识符使用连字符格式,且两者不同。
- 禁用 AIBuds AI SDK 内部录音时,宿主 App 需要提供外部 PCM。如果音频来自已连接设备,该设备需遵循
DeviceAudioRecordingAPI。
使用 AI 辅助实现
使用 AI 开发
让 AI 帮助实现此工作流
使用官方“实现 AIBuds 同声传译”技能,根据你的 App 完成实现。
请阅读并遵循 https://docs-aibuds.github.io/zh-Hans/skills/implement-aibuds-simultaneous-interpretation,使用该技能在当前 iOS 项目中完成“实现 AIBuds 同声传译”,并验证结果。API 参考
框架
AIBudsAI.xcframework
导入
- Swift
- Objective-C
import AIBuds
import AIBudsAI
import AIBudsAIFoundation#import <AIBuds/AIBuds-Swift.h>
#import <AIBudsAI/AIBudsAI-Swift.h>声明
- Swift
- Objective-C
/// Starts a simultaneous interpretation session.
/// - Parameters:
/// - config: The session configuration.
/// - onStartSuccess: Called with the started session.
/// - onStartFailure: Called when the session cannot start.
/// - onStopByInterruption: Called when an interruption stops the session.
/// - onException: Called for a recoverable session exception. The app decides
/// whether the session should stop.
/// - streamResultHandler: Called with incremental interpretation results.
/// - onEvent: Called for session-level events.
/// - onFinish: Called with the optional final report.
public static func startSimultaneousInterpretation(_ config: SimultaneousInterpretationConfig = .default,
onStartSuccess: ((_ session: SimultaneousInterpretationSessionConvertible) -> Void)? = nil,
onStartFailure: ((_ error: NSError) -> Void)? = nil,
onStopByInterruption: ((_ error: NSError?) -> Void)? = nil,
onException: ((_ error: NSError) -> Void)? = nil,
streamResultHandler: ((
_ isFinal: Bool,
_ response: SimultaneousInterpretationDataModel?,
_ error: Error?
) -> Void)? = nil,
onEvent: ((_ event: SimultaneousInterpretationEventModel) -> Void)? = nil,
onFinish: ((_ report: SimultaneousInterpretationReportModel?) -> Void)? = nil)
/// Stops the current simultaneous interpretation session.
public static func stopSimultaneousInterpretation()/// Starts a simultaneous interpretation session.
/// - Parameters:
/// - config: The session configuration.
/// - onStartSuccess: Called with the started session.
/// - onStartFailure: Called when the session cannot start.
/// - onStopByInterruption: Called when an interruption stops the session.
/// - onException: Called for a recoverable session exception.
/// - streamResultHandler: Called with incremental interpretation results.
/// - onEvent: Called for session-level events.
/// - onFinish: Called with the optional final report.
+ (void)startSimultaneousInterpretationWithConfig:(AIBudsSimultaneousInterpretationConfig * _Nonnull)config
onStartSuccess:(void (^ _Nullable)(id <AIBudsSimultaneousInterpretationSessionConvertible> _Nonnull))onStartSuccess
onStartFailure:(void (^ _Nullable)(NSError * _Nonnull))onStartFailure
onStopByInterruption:(void (^ _Nullable)(NSError * _Nullable))onStopByInterruption
onException:(void (^ _Nullable)(NSError * _Nonnull))onException
streamResultHandler:(void (^ _Nullable)(BOOL, AIBudsSimultaneousInterpretationDataModel * _Nullable, NSError * _Nullable))streamResultHandler
onEvent:(void (^ _Nullable)(AIBudsSimultaneousInterpretationEventModel * _Nonnull))onEvent
onFinish:(void (^ _Nullable)(AIBudsSimultaneousInterpretationReportModel * _Nullable))onFinish;
/// Stops the current simultaneous interpretation session.
+ (void)stopSimultaneousInterpretation;请参阅 startSimultaneousInterpretation 和 stopSimultaneousInterpretation。
配置
SimultaneousInterpretationConfig 提供:
| 属性 | 默认值 | 描述 |
|---|---|---|
sourceLanguage | App 语言 | 可选源语言;空字符串启用自动检测。 |
targetLanguage | en-US | 必填目标语言。 |
enableTTS | true | 是否合成译文语音。 |
enableVoicePlayback | true | 是否启用合成语音播放。 |
usesInternalAudioRecording | true | 是否由 AIBuds AI SDK 为当前会话录音。 |
preferSpeakerOutput | false | 是否优先使用扬声器输出。 |
usesInternalAudioRecording 为 false 时,AIBuds AI SDK 不会为该会话采集音频。宿主 App 必须保留返回的 session,并通过 appendInt16PCM(_:isFinal:) 或 appendAudioPCMBuffer(_:isFinal:) 输入外部 PCM。
使用示例
- Swift
- Objective-C
let config = SimultaneousInterpretationConfig.default
config.sourceLanguage = "zh-CN"
config.targetLanguage = "en-US"
config.usesInternalAudioRecording = true
config.preferSpeakerOutput = false
AIBudsAISDK.startSimultaneousInterpretation(
config,
onStartSuccess: { session in
currentSession = session
},
onStartFailure: { error in
print("Unable to start: \(error.localizedDescription)")
},
onStopByInterruption: { error in
print(error?.localizedDescription ?? "Session interrupted")
currentSession = nil
},
onException: { error in
print("Session exception: \(error.localizedDescription)")
},
streamResultHandler: { isFinal, response, error in
if let error {
print(error.localizedDescription)
return
}
guard let response else { return }
if response.isSourceTextDefinite {
print("Source: \(response.sourceText ?? "")")
}
if response.isTargetTextDefinite {
print("Target: \(response.targetText ?? "")")
}
if isFinal { print("Final result") }
},
onEvent: { event in
print(event)
},
onFinish: { report in
currentSession = nil
print(report ?? "No report")
}
)AIBudsSimultaneousInterpretationConfig *config =
[AIBudsSimultaneousInterpretationConfig defaultConfig];
config.sourceLanguage = @"zh-CN";
config.targetLanguage = @"en-US";
config.usesInternalAudioRecording = YES;
config.preferSpeakerOutput = NO;
[AIBudsAISDK startSimultaneousInterpretationWithConfig:config
onStartSuccess:^(id<AIBudsSimultaneousInterpretationSessionConvertible> session) {
self.currentSession = session;
}
onStartFailure:^(NSError *error) {
NSLog(@"Unable to start: %@", error.localizedDescription);
}
onStopByInterruption:^(NSError *error) {
NSLog(@"%@", error.localizedDescription ?: @"Session interrupted");
self.currentSession = nil;
}
onException:^(NSError *error) {
NSLog(@"Session exception: %@", error.localizedDescription);
}
streamResultHandler:^(
BOOL isFinal, AIBudsSimultaneousInterpretationDataModel *response, NSError *error) {
if (error != nil) {
NSLog(@"%@", error.localizedDescription);
return;
}
if (response.isSourceTextDefinite) {
NSLog(@"Source: %@", response.sourceText ?: @"");
}
if (response.isTargetTextDefinite) {
NSLog(@"Target: %@", response.targetText ?: @"");
}
}
onEvent:^(AIBudsSimultaneousInterpretationEventModel *event) {
NSLog(@"%@", event);
}
onFinish:^(AIBudsSimultaneousInterpretationReportModel *report) {
self.currentSession = nil;
NSLog(@"%@", report);
}];session.isRecordingInternally 为 false 时,外部音频链路由宿主 App 负责。Demo 在 onStartSuccess 后启动设备端 AI 录音,将每批解码 PCM 转发给 session.appendInt16PCM(_:isFinal:),停止时先结束设备录音,再调用 AIBudsAISDK.stopSimultaneousInterpretation()。
注意事项
- 保留启动时返回的
SimultaneousInterpretationSessionConvertible,用于跟踪当前会话及其录音模式。 onException不一定会停止会话。请决定继续会话还是调用stopSimultaneousInterpretation()。- 使用
sourceTextSequence和targetTextSequence对确定片段排序,不要盲目追加每次增量回调。 SimultaneousInterpretationDataModel可通过相对文件、完整文件路径或 Base64 PCM 数据提供 TTS 音频。