跳到主要内容

同声传译

启动长时间运行的语音同声传译会话,接收增量源文本、译文、可选 TTS 音频、事件和最终报告。

有关 SimultaneousInterpretationEventType 生命周期以及 onEvent 的处理方式,请参阅 AI 会话事件

Animated workflow

同声传译会话生命周期

协调服务商启动、当前音频源、有序增量结果、中断处理和最终停止。

宿主 App

配置语言

设置兼容的源语言、目标语言以及 TTS 和播放选项。

AI 服务

启动会话

启动服务商提供的同声传译服务。

宿主 App

保留会话

保存返回的会话,并检查是否由 AIBuds AI SDK 在内部录音。

宿主 App + 设备

提供音频

禁用 SDK 内部录音时,向会话输入外部 PCM;设备录音只是其中一种音频来源。

AI 服务 → App

流式接收结果

对确定的源语言和目标语言片段排序,并处理可选 TTS 音频。

增量结果
宿主 App

处理运行时事件

处理事件、可恢复异常和中断触发的停止。

设备

停止外部音频

设备录音启用时,先停止录音再停止同声传译。

AI 服务

停止同声传译

请求停止当前同声传译会话。

最终回调

结束会话

处理可选报告,并清除保留的当前会话。

可恢复的 onException 回调不代表会话已自动停止。

前提条件

  • 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
import AIBuds
import AIBudsAI
import AIBudsAIFoundation

声明

Swift
/// 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()

请参阅 startSimultaneousInterpretationstopSimultaneousInterpretation

配置

SimultaneousInterpretationConfig 提供:

属性默认值描述
sourceLanguageApp 语言可选源语言;空字符串启用自动检测。
targetLanguageen-US必填目标语言。
enableTTStrue是否合成译文语音。
enableVoicePlaybacktrue是否启用合成语音播放。
usesInternalAudioRecordingtrue是否由 AIBuds AI SDK 为当前会话录音。
preferSpeakerOutputfalse是否优先使用扬声器输出。

usesInternalAudioRecordingfalse 时,AIBuds AI SDK 不会为该会话采集音频。宿主 App 必须保留返回的 session,并通过 appendInt16PCM(_:isFinal:)appendAudioPCMBuffer(_:isFinal:) 输入外部 PCM。

使用示例

Swift
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")
    }
)

session.isRecordingInternallyfalse 时,外部音频链路由宿主 App 负责。Demo 在 onStartSuccess 后启动设备端 AI 录音,将每批解码 PCM 转发给 session.appendInt16PCM(_:isFinal:),停止时先结束设备录音,再调用 AIBudsAISDK.stopSimultaneousInterpretation()

注意事项

  • 保留启动时返回的 SimultaneousInterpretationSessionConvertible,用于跟踪当前会话及其录音模式。
  • onException 不一定会停止会话。请决定继续会话还是调用 stopSimultaneousInterpretation()
  • 使用 sourceTextSequencetargetTextSequence 对确定片段排序,不要盲目追加每次增量回调。
  • SimultaneousInterpretationDataModel 可通过相对文件、完整文件路径或 Base64 PCM 数据提供 TTS 音频。