对话翻译
为当前说话者启动短时同声传译会话,并为另一位参与者交换源语言和目标语言,从而构建双人轮次式翻译体验。
这是 ConversationTranslationDemoController 演示的产品流程,不会引入独立的 ConversationTranslationAPI。
Animated workflow
一个对话轮次
每个说话轮次持有一个传译会话。对方轮次需要反转语言方向,并且两个轮次不能并发。
前提条件
- 满足同声传译中的所有前提条件。
- 使用不同的连字符格式源语言和目标语言标识符。
- 在宿主 App 中记录当前说话者、当前会话、启停状态和已经展示的轮次消息。
- 防止两位说话者同时启动会话。
使用 AI 辅助实现
使用 AI 开发
让 AI 帮助实现此工作流
使用官方“实现 AIBuds 对话翻译”技能,根据你的 App 完成实现。
请阅读并遵循 https://docs-aibuds.github.io/zh-Hans/skills/implement-aibuds-conversation-translation,使用该技能在当前 iOS 项目中完成“实现 AIBuds 对话翻译”,并验证结果。API 参考
对话翻译使用 startSimultaneousInterpretation、stopSimultaneousInterpretation 和 SimultaneousInterpretationConfig。
完整公开声明和回调契约请参阅同声传译。
使用示例
开始说话轮次
Demo 将“我”映射为 myLanguage → partnerLanguage,将“对方”映射为相反方向。以下辅助方法接收已经确定的方向,使该产品状态保持明确。
- Swift
- Objective-C
func startConversationTurn(source: String, target: String) {
guard currentSession == nil, source != target else { return }
let config = SimultaneousInterpretationConfig.default
config.sourceLanguage = source
config.targetLanguage = target
config.usesInternalAudioRecording = true
config.preferSpeakerOutput = true
config.enableVoicePlayback = true
AIBudsAISDK.startSimultaneousInterpretation(
config,
onStartSuccess: { session in
currentSession = session
setTurnActive(true)
},
onStartFailure: { error in
currentSession = nil
setTurnActive(false)
show(error)
},
onStopByInterruption: { error in
currentSession = nil
setTurnActive(false)
if let error { show(error) }
},
onException: { error in
showRecoverable(error)
},
streamResultHandler: { _, response, error in
if let error {
show(error)
return
}
guard let response else { return }
if response.isSourceTextDefinite { renderSource(response) }
if response.isTargetTextDefinite { renderTarget(response) }
},
onEvent: { event in
handle(event)
},
onFinish: { report in
currentSession = nil
setTurnActive(false)
save(report)
}
)
}- (void)startConversationTurnFrom:(NSString *)source to:(NSString *)target {
if (self.currentSession != nil || [source isEqualToString:target])
return;
AIBudsSimultaneousInterpretationConfig *config =
[AIBudsSimultaneousInterpretationConfig defaultConfig];
config.sourceLanguage = source;
config.targetLanguage = target;
config.usesInternalAudioRecording = YES;
config.preferSpeakerOutput = YES;
config.enableVoicePlayback = YES;
[AIBudsAISDK startSimultaneousInterpretationWithConfig:config
onStartSuccess:^(id<AIBudsSimultaneousInterpretationSessionConvertible> session) {
self.currentSession = session;
[self setTurnActive:YES];
}
onStartFailure:^(NSError *error) {
self.currentSession = nil;
[self setTurnActive:NO];
[self showError:error];
}
onStopByInterruption:^(NSError *error) {
self.currentSession = nil;
[self setTurnActive:NO];
if (error != nil)
[self showError:error];
}
onException:^(NSError *error) {
[self showRecoverableError:error];
}
streamResultHandler:^(
BOOL isFinal, AIBudsSimultaneousInterpretationDataModel *response, NSError *error) {
if (error != nil) {
[self showError:error];
return;
}
if (response.isSourceTextDefinite)
[self renderSource:response];
if (response.isTargetTextDefinite)
[self renderTarget:response];
}
onEvent:^(AIBudsSimultaneousInterpretationEventModel *event) {
[self handleEvent:event];
}
onFinish:^(AIBudsSimultaneousInterpretationReportModel *report) {
self.currentSession = nil;
[self setTurnActive:NO];
[self saveReport:report];
}];
}停止当前轮次
当 currentSession.isRecordingInternally 为 false 时,应先停止设备端 AI 录音,再停止服务,以保证最终音频按顺序传递。
- Swift
- Objective-C
func stopConversationTurn() {
guard let session = currentSession else { return }
if !session.isRecordingInternally,
let recordingDevice = device as? DeviceAudioRecordingAPI
{
recordingDevice.stopAIAudioRecording(.onSite) { _, _ in
AIBudsAISDK.stopSimultaneousInterpretation()
}
} else {
AIBudsAISDK.stopSimultaneousInterpretation()
}
}- (void)stopConversationTurn {
if (self.currentSession == nil)
return;
if (!self.currentSession.isRecordingInternally) {
id<AIBudsDeviceAudioRecordingAPI> recordingDevice =
(id<AIBudsDeviceAudioRecordingAPI>)self.device;
if ([recordingDevice conformsToProtocol:@protocol(AIBudsDeviceAudioRecordingAPI)]) {
[recordingDevice
stopAIAudioRecordingWithScene:AIBudsRecordingSceneOnSite
completion:^(__unused BOOL success, __unused NSError *error) {
[AIBudsAISDK stopSimultaneousInterpretation];
}];
return;
}
}
[AIBudsAISDK stopSimultaneousInterpretation];
}错误处理
分别处理启动失败、中断、可恢复异常、流错误、设备录音失败和最终完成。如果设备录音仍在启动时收到停止请求,应推迟停止服务,直到设备启动回调完成;Demo 对该竞态进行了显式跟踪。
注意事项
- 一次对话可以包含多个轮次,但同一时间只能有一个活动传译会话。
- 仅在没有活动轮次时交换语言。
- 使用源序列和目标序列字段组织确定片段,不要盲目追加每个回调。
- 使用外部设备录音时,应在启动设备音频前,将保留的传译会话注册到应用的 PCM 转发路径,避免丢失第一个数据包。