대화 번역
현재 화자를 위한 짧은 동시통역 세션을 시작하고 상대방 차례에는 원문과 번역 언어를 바꾸어 두 사람이 번갈아 사용하는 대화 번역을 구현합니다.
이 기능은 ConversationTranslationDemoController에서 보여 주는 제품 작업 흐름이며 별도의 ConversationTranslationAPI를 추가하지 않습니다.
한 번의 대화 차례
각 화자의 차례마다 통역 세션 하나를 사용합니다. 상대방 차례에는 언어 방향을 반대로 설정하고 두 차례를 동시에 실행하지 마세요.
사전 요구 사항
- 동시통역의 모든 사전 요구 사항을 충족합니다.
- 서로 다른 하이픈 형식의 원문 및 번역 언어 식별자를 사용합니다.
- 호스트 앱에서 현재 화자, 활성 세션, 시작 및 중지 상태, 차례별 메시지를 추적합니다.
- 두 화자가 동시에 세션을 시작하지 못하게 합니다.
AI를 활용해 구현
AI로 이 워크플로 구현
공식 “AIBuds 대화 번역 구현” 스킬을 사용해 앱에 맞게 구현하세요.
https://docs-aibuds.github.io/ko/skills/implement-aibuds-conversation-translation을 읽고 지침을 따르세요. 이 스킬로 “AIBuds 대화 번역 구현”을 이 iOS 프로젝트에 구현하고 검증하세요.API Reference
대화 번역은 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 전달 경로에 등록합니다.