> For the complete documentation index, see [llms.txt](https://docs.convai.com/api-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unreal-engine-plugin/features/lip-sync/record-and-replay-lip-sync.md).

# 录制并回放口型同步

在 Convai 对话期间捕获实时口型同步序列，并在之后的过场动画、缓存响应或离线预览中回放，无需重新流式传输。

通过 `UConvaiFaceSyncComponent` 的录制 API，可以捕获实时对话期间收到的混合形状帧，并按需重放，而无需重新从 Convai 流式传输。该 API 可用于反复播放相同口型同步序列的过场动画、在触发响应前对其进行预热，或用于面部动画的调试和离线预览。

{% hint style="warning" %}
录制 API—— `StartRecordingLipSync`, `FinishRecordingLipSync`, `PlayRecordedLipSync`, `IsPlaying`和 `GetCurrentFrame` ——仅支持 C++。这些方法没有使用 `UFUNCTION` 修饰，无法从蓝图中访问。
{% endhint %}

### 何时使用录制 API

| 使用场景         | 处理方式                                          |
| ------------ | --------------------------------------------- |
| 角色台词固定的过场动画  | 在开发期间录制一次，存储序列，并在过场动画中重放                      |
| 预热的响应缓存      | 在首次播放时录制响应，后续遇到相同响应时重放                        |
| 离线预览面部动画     | 在 PIE 会话期间录制，使用以下方法检查帧数据： `GetCurrentFrame()` |
| 实时流式传输（实时对话） | 不要使用录制 API——AnimGraph 节点会自动处理实时帧              |

### 前提条件

* 该 Actor 具有一个 `Convai Face Sync` 组件和一个正常工作的 `Convai Chatbot` 组件。
* 角色在对话期间已能实现口型同步（如果尚未实现，请参阅 [口型同步快速入门](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unreal-engine-plugin/features/lip-sync/lip-sync-quick-start.md) ）。

### 录制序列

{% stepper %}
{% step %}

#### 在发言开始前启动录制

在 `StartRecordingLipSync()` 方法所属的 `UConvaiFaceSyncComponent` 实例上调用该方法。从此时起，Convai 传送的每一帧混合形状数据除了应用到面部，还会在内部缓冲。

```cpp
// 伪代码
UConvaiFaceSyncComponent* FaceSync = MyCharacter->FindComponentByClass<UConvaiFaceSyncComponent>();
if (FaceSync)
{
    FaceSync->StartRecordingLipSync();
}
```

{% endstep %}

{% step %}

#### 让角色说话

像平常一样触发对话。角色会说话，面部也会随之产生动画。组件会在后台捕获收到的帧，录制过程中无需额外调用。
{% endstep %}

{% step %}

#### 停止录制并存储结果

发言结束时，调用 `FinishRecordingLipSync()`。该方法会停止捕获并返回一个 `FAnimationSequenceBP` 其中包含完整的、按帧索引的 BlendShape 帧列表、总时长和帧率。

```cpp
// 伪代码
FAnimationSequenceBP CachedSequence = FaceSync->FinishRecordingLipSync();
```

将此值存储为 Actor 或组件的成员变量，以便稍后重播。此时，你已获得对话轮次期间播放的完整面部动画。
{% endstep %}
{% endstepper %}

### 重播录制的序列

调用 `PlayRecordedLipSync` （在 `UConvaiFaceSyncComponent` 实例上）即可重播存储的序列。该组件会将录制的帧传递给 AnimGraph 节点，方式与实时流期间完全相同，且无需处于活动状态的 Convai 对话流。

#### PlayRecordedLipSync 参数

| 参数                  | 类型                     | 说明                                   |
| ------------------- | ---------------------- | ------------------------------------ |
| `RecordedLipSync`   | `FAnimationSequenceBP` | 由 `FinishRecordingLipSync`.          |
| `StartFrame`        | `int32`                | 要重播的第一帧的索引。传入 `0` 即可从头开始播放。          |
| `EndFrame`          | `int32`                | 要重播的最后一帧的索引。传入 `-1` 即可播放到序列末尾。       |
| `OverwriteDuration` | `float`                | 以秒为单位覆盖序列中存储的播放时长。传入 `0.0` 即可使用原始时长。 |

`PlayRecordedLipSync` 返回 `bool` —— `true` 表示序列已成功开始播放。返回 `false` 表示序列为空或无效、录制仍在进行， `StartFrame` 大于最后一帧的索引，或者 `EndFrame` 大于 `0` 且小于 `StartFrame`.

```cpp
// 伪代码
bool bStarted = FaceSync->PlayRecordedLipSync(CachedSequence, 0, -1, 0.0f);
```

### 监控播放状态

录制的序列播放期间，可使用以下 C++ 方法监控组件：

| 方法                  | 返回                   | 说明                                               |
| ------------------- | -------------------- | ------------------------------------------------ |
| `IsPlaying() const` | `bool`               | `true` 表示组件当前正在重播录制序列或实时序列中的帧。                   |
| `GetCurrentFrame()` | `TMap<FName, float>` | 当前正在应用的帧所对应的 BlendShape 名称到权重映射。可用于利用相同数据驱动辅助系统。 |

### C++ 流程示例

典型的过场动画用法如下：

```cpp
// 伪代码
// 1. 在语音轮次开始之前或开始时：
FaceSync->StartRecordingLipSync();

// 2. 角色开始说话——录制会在后台自动进行。

// 3. 语音轮次结束时：
FAnimationSequenceBP CachedSequence = FaceSync->FinishRecordingLipSync();

// 4. 想要重播序列时（例如，在过场动画触发时）：
FaceSync->PlayRecordedLipSync(CachedSequence, 0, -1, 0.0f);
```

{% hint style="info" %}
重播序列不会再次触发音频。请单独播放匹配的音频片段，并对齐播放开始时间，以保持面部动作与语音同步。
{% endhint %}

### 后续步骤

{% content-ref url="/pages/8b7003d8fdf40c7b56b0fd2a54376bfc5105903c" %}
[Face Sync 组件参考](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unreal-engine-plugin/features/lip-sync/face-sync-component-reference.md)
{% endcontent-ref %}

{% content-ref url="/pages/1d4e6a68b8b0634dd00381e03cb9447a556b758c" %}
[口型同步使用示例](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unreal-engine-plugin/features/lip-sync/lip-sync-usage-examples.md)
{% endcontent-ref %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unreal-engine-plugin/features/lip-sync/record-and-replay-lip-sync.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
