Files
XCEngine/docs/api/components/audio-source-component/audio-source-component.md
ssdfasd a1804f4cb0 docs: 更新 resources, rhi, scene 模块及新增 camera-component 方法文档
- resources: 更新 asyncloader, audioclip, mesh-import-settings, texture-loader 文档
- rhi: 更新 opengl render-target-view 文档
- components: 新增 camera-component 全部方法文档 (15个文件)
2026-03-26 02:00:45 +08:00

122 lines
4.0 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# AudioSourceComponent
**命名空间**: `XCEngine::Components`
**类型**: `class`
**头文件**: `XCEngine/Components/AudioSourceComponent.h`
**描述**: 音频源组件负责声音播放、3D 空间化、衰减和多普勒效应。
## 概述
AudioSourceComponent 是 XCEngine ECS 系统中的音频源组件,用于在场景中播放声音。它支持 3D 空间化、声音衰减、多普勒效应、混响区域混合等功能。该组件需要配合 AudioListenerComponent 使用来接收声音并进行音频处理。
## 公共方法
### 播放控制
| 方法 | 描述 |
|------|------|
| [`Play`](play.md) | 开始播放音频 |
| [`Pause`](pause.md) | 暂停播放 |
| [`Stop`](stop.md) | 停止播放 |
| [`IsPlaying`](is-playing.md) | 检查是否正在播放 |
| [`IsPaused`](is-paused.md) | 检查是否已暂停 |
### 音频剪辑
| 方法 | 描述 |
|------|------|
| [`SetClip`](set-clip.md) | 设置音频剪辑 |
| [`GetClip`](get-clip.md) | 获取当前音频剪辑 |
### 音频属性
| 方法 | 描述 |
|------|------|
| [`SetVolume`](../../audio/audio-mixer/set-volume.md) | 设置音量 |
| [`GetVolume`](get-volume.md) | 获取音量 |
| [`SetPitch`](set-pitch.md) | 设置音调 |
| [`GetPitch`](get-pitch.md) | 获取音调 |
| [`SetPan`](set-pan.md) | 设置平移 |
| [`GetPan`](get-pan.md) | 获取平移 |
| [`SetLooping`](set-looping.md) | 设置循环播放 |
| [`IsLooping`](is-looping.md) | 检查是否循环播放 |
### 3D 空间化
| 方法 | 描述 |
|------|------|
| [`SetSpatialize`](set-spatialize.md) | 设置是否启用 3D 空间化 |
| [`IsSpatialize`](is-spatialize.md) | 检查是否启用 3D 空间化 |
| [`Set3DParams`](../../audio/audio-mixer/set-3d-params.md) | 设置 3D 音频参数 |
| [`Get3DParams`](get-3d-params.md) | 获取 3D 音频参数 |
| [`SetDopplerLevel`](set-doppler-level.md) | 设置多普勒效应等级 |
| [`GetDopplerLevel`](get-doppler-level.md) | 获取多普勒效应等级 |
| [`SetSpread`](set-spread.md) | 设置扩散角度 |
| [`GetSpread`](get-spread.md) | 获取扩散角度 |
| [`SetReverbZoneMix`](set-reverb-zone-mix.md) | 设置混响区域混合 |
| [`GetReverbZoneMix`](get-reverb-zone-mix.md) | 获取混响区域混合 |
### 输出混音器
| 方法 | 描述 |
|------|------|
| [`SetOutputMixer`](../../audio/audio-mixer/set-output-mixer.md) | 设置输出混音器 |
| [`GetOutputMixer`](get-output-mixer.md) | 获取输出混音器 |
### 播放时间
| 方法 | 描述 |
|------|------|
| [`SetTime`](set-time.md) | 设置播放位置 |
| [`GetTime`](get-time.md) | 获取当前播放位置 |
| [`GetDuration`](../../resources/audioclip/get-duration.md) | 获取音频总时长 |
### 能量检测
| 方法 | 描述 |
|------|------|
| [`GetEnergy`](../audio-listener-component/get-energy.md) | 获取当前能量值 |
| [`StartEnergyDetect`](start-energy-detect.md) | 开始能量检测 |
| [`StopEnergyDetect`](stop-energy-detect.md) | 停止能量检测 |
| [`IsEnergyDetecting`](is-energy-detecting.md) | 检查是否正在能量检测 |
## 使用示例
```cpp
#include <XCEngine/Components/AudioSourceComponent.h>
#include <XCEngine/Components/GameObject.h>
#include <XCEngine/Resources/AudioClip/AudioClip.h>
using namespace XCEngine::Components;
void PlaySound(GameObject* audioObject) {
auto audioSource = audioObject->AddComponent<AudioSourceComponent>();
audioSource->SetVolume(0.8f);
audioSource->SetPitch(1.0f);
audioSource->SetLooping(false);
audioSource->SetSpatialize(true);
audioSource->Play();
}
void Setup3DAudio(GameObject* audioObject) {
auto audioSource = audioObject->AddComponent<AudioSourceComponent>();
Audio::Audio3DParams params;
params.dopplerLevel = 1.0f;
params.spread = 0.0f;
params.reverbZoneMix = 1.0f;
audioSource->Set3DParams(params);
}
```
## 相关文档
- [Components 模块总览](../components.md) - Components 模块总览
- [Component](../component/component.md) - 组件基类
- [AudioListenerComponent](../audio-listener-component/audio-listener-component.md) - 音频监听组件