# 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`](get-energy.md) | 获取当前能量值 | | [`StartEnergyDetect`](start-energy-detect.md) | 开始能量检测 | | [`StopEnergyDetect`](stop-energy-detect.md) | 停止能量检测 | | [`IsEnergyDetecting`](is-energy-detecting.md) | 检查是否正在能量检测 | ## 使用示例 ```cpp #include #include #include using namespace XCEngine::Components; void PlaySound(GameObject* audioObject) { auto audioSource = audioObject->AddComponent(); audioSource->SetVolume(0.8f); audioSource->SetPitch(1.0f); audioSource->SetLooping(false); audioSource->SetSpatialize(true); audioSource->Play(); } void Setup3DAudio(GameObject* audioObject) { auto audioSource = audioObject->AddComponent(); 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) - 音频监听组件