Files
XCEngine/docs/api/components/audio-source/audio-source-component.md
ssdfasd 161a0896d5 docs: 添加 Audio 模块和 Components 模块 API 文档
- 新增 Audio 模块文档 (54 个文件)
  - AudioSystem 单例类及 20 个方法页
  - AudioMixer 混音器类及 11 个方法页
  - IAudioBackend、IAudioEffect 接口
  - FFTFilter、Reverbation、Equalizer、HRTF 效果类
  - WASAPIBackend Windows 后端
  - AudioConfig、Audio3DParams 等结构体
  - 9 个枚举类型文档
- 新增 Components 模块文档 (3 个文件)
  - AudioSourceComponent 音频源组件
  - AudioListenerComponent 音频监听器组件
- 更新 docs/api/main.md 添加模块导航
2026-03-22 01:56:16 +08:00

4.1 KiB
Raw Blame History

AudioSourceComponent

命名空间: XCEngine::Components

类型: class

头文件: XCEngine/Components/AudioSourceComponent.h

描述: 音频源组件,负责在 ECS 实体上播放音频。

概述

AudioSourceComponent 是 XCEngine ECS 系统中的音频源组件附加到游戏实体上后可以播放音频片段AudioClip。它支持 3D 空间化定位、音量、音调、循环、展开角度、多普勒效应等丰富的音频播放参数。该组件需要与 AudioListenerComponent 配合使用以实现 3D 空间音效。

公共方法

方法 描述
AudioSourceComponent 构造函数
~AudioSourceComponent 析构函数
Play 开始播放
Pause 暂停播放
Stop 停止播放
IsPlaying 检查是否正在播放
IsPaused 检查是否已暂停
SetClip 设置音频片段
GetClip 获取音频片段
SetVolume 设置音量
GetVolume 获取音量
SetPitch 设置音调
GetPitch 获取音调
SetPan 设置声像
GetPan 获取声像
SetLooping 设置循环播放
IsLooping 检查循环状态
SetSpatialize 设置是否启用空间化
IsSpatialize 检查空间化状态
Set3DParams 设置 3D 参数
Get3DParams 获取 3D 参数
SetDopplerLevel 设置多普勒等级
GetDopplerLevel 获取多普勒等级
SetSpread 设置展开角度
GetSpread 获取展开角度
SetReverbZoneMix 设置混响区域混合
GetReverbZoneMix 获取混响区域混合
SetOutputMixer 设置输出混音器
GetOutputMixer 获取输出混音器
SetTime 设置播放时间
GetTime 获取播放时间
GetDuration 获取音频时长
GetEnergy 获取音频能量
StartEnergyDetect 开始能量检测
StopEnergyDetect 停止能量检测
IsEnergyDetecting 检查能量检测状态

组件方法

方法 描述
Update 每帧更新
OnEnable 启用时回调
OnDisable 禁用时回调
OnDestroy 销毁时回调
GetName 获取组件名称

使用示例

#include <XCEngine/Components/AudioSourceComponent.h>
#include <XCEngine/Resources/ResourceManager.h>

using namespace XCEngine::Components;

void PlaySoundEffect(Entity entity, const char* soundPath) {
    AudioSourceComponent* audioSource = entity.AddComponent<AudioSourceComponent>();
    
    auto clip = Resources::ResourceManager::Get().Load<Resources::AudioClip>(soundPath);
    audioSource->SetClip(clip.Get());
    audioSource->SetVolume(0.8f);
    audioSource->SetLooping(false);
    audioSource->SetSpatialize(true);
    
    audioSource->Play();
}

void PlayLoopingMusic(Entity entity, const char* musicPath) {
    AudioSourceComponent* audioSource = entity.AddComponent<AudioSourceComponent>();
    
    auto clip = Resources::ResourceManager::Get().Load<Resources::AudioClip>(musicPath);
    audioSource->SetClip(clip.Get());
    audioSource->SetVolume(0.5f);
    audioSource->SetLooping(true);
    audioSource->SetSpatialize(false);
    
    audioSource->Play();
}

相关文档