From b7739dd1dfbc600e4b69c40af0716d989351490d Mon Sep 17 00:00:00 2001
From: GeWuYou <95328647+GeWuYou@users.noreply.github.com>
Date: Wed, 17 Dec 2025 13:04:44 +0800
Subject: [PATCH] =?UTF-8?q?feat(audio):=20=E6=B7=BB=E5=8A=A0=E5=A4=9A?=
=?UTF-8?q?=E7=A7=8D=E9=9F=B3=E9=A2=91=E6=92=AD=E6=94=BE=E7=B1=BB=E5=9E=8B?=
=?UTF-8?q?=E5=92=8C=E9=9F=B3=E9=87=8F=E6=8E=A7=E5=88=B6=E6=96=B9=E6=B3=95?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
- 新增PlaySfx、PlayVoice和PlayAmbient方法用于播放不同类型音频
- 为音乐、音效、语音和环境音效添加独立的音量设置与获取接口
- 扩展IAudioManagerSystem接口以支持新的音频控制功能
- 在AbstractAudioManagerSystem中实现新增的音频播放逻辑
- 提供GetMusicVolume、GetSoundVolume等获取当前音量的方法
- [no tag]
---
.../system/AbstractAudioManagerSystem.cs | 111 ++++++++++++++++++
.../system/IAudioManagerSystem.cs | 78 ++++++++++++
2 files changed, 189 insertions(+)
diff --git a/GFramework.Core.Godot/system/AbstractAudioManagerSystem.cs b/GFramework.Core.Godot/system/AbstractAudioManagerSystem.cs
index 0df032f..077686f 100644
--- a/GFramework.Core.Godot/system/AbstractAudioManagerSystem.cs
+++ b/GFramework.Core.Godot/system/AbstractAudioManagerSystem.cs
@@ -203,6 +203,63 @@ public abstract class AbstractAudioManagerSystem : AbstractSystem, IAudioManager
player.Play();
}
+ ///
+ /// 播放特效音效
+ ///
+ /// 音频文件路径
+ /// 音量大小,范围0-1
+ /// 音调调整
+ public virtual void PlaySfx(string audioPath, float volume = 1.0f, float pitch = 1.0f)
+ {
+ if (AvailableSoundPlayers.Count == 0) return;
+
+ var audioStream = ResourceLoadSystem?.LoadResource(audioPath);
+ if (audioStream == null) return;
+
+ var player = AvailableSoundPlayers.Dequeue();
+ player.Stream = audioStream;
+ player.VolumeDb = LinearToDb(volume * SfxVolume * MasterVolume);
+ player.Play();
+ }
+
+ ///
+ /// 播放语音
+ ///
+ /// 音频文件路径
+ /// 音量大小,范围0-1
+ /// 音调调整
+ public virtual void PlayVoice(string audioPath, float volume = 1.0f, float pitch = 1.0f)
+ {
+ if (AvailableSoundPlayers.Count == 0) return;
+
+ var audioStream = ResourceLoadSystem?.LoadResource(audioPath);
+ if (audioStream == null) return;
+
+ var player = AvailableSoundPlayers.Dequeue();
+ player.Stream = audioStream;
+ player.VolumeDb = LinearToDb(volume * VoiceVolume * MasterVolume);
+ player.Play();
+ }
+
+ ///
+ /// 播放环境音效
+ ///
+ /// 音频文件路径
+ /// 音量大小,范围0-1
+ /// 音调调整
+ public virtual void PlayAmbient(string audioPath, float volume = 1.0f, float pitch = 1.0f)
+ {
+ if (AvailableSoundPlayers.Count == 0) return;
+
+ var audioStream = ResourceLoadSystem?.LoadResource(audioPath);
+ if (audioStream == null) return;
+
+ var player = AvailableSoundPlayers.Dequeue();
+ player.Stream = audioStream;
+ player.VolumeDb = LinearToDb(volume * AmbientVolume * MasterVolume);
+ player.Play();
+ }
+
///
/// 通过资源ID播放音效
///
@@ -272,6 +329,15 @@ public abstract class AbstractAudioManagerSystem : AbstractSystem, IAudioManager
MusicPlayer.VolumeDb = LinearToDb(MusicVolume * MasterVolume);
}
}
+
+ ///
+ /// 获取背景音乐音量
+ ///
+ /// 音量大小,范围0-1
+ public virtual float GetMusicVolume()
+ {
+ return MusicVolume;
+ }
///
/// 设置音效音量
@@ -281,6 +347,15 @@ public abstract class AbstractAudioManagerSystem : AbstractSystem, IAudioManager
{
SoundVolume = volume;
}
+
+ ///
+ /// 获取音效音量
+ ///
+ /// 音量大小,范围0-1
+ public virtual float GetSoundVolume()
+ {
+ return SoundVolume;
+ }
///
/// 设置主音量
@@ -296,6 +371,15 @@ public abstract class AbstractAudioManagerSystem : AbstractSystem, IAudioManager
MusicPlayer.VolumeDb = LinearToDb(MusicVolume * MasterVolume);
}
}
+
+ ///
+ /// 获取主音量
+ ///
+ /// 音量大小,范围0-1
+ public virtual float GetMasterVolume()
+ {
+ return MasterVolume;
+ }
///
/// 设置SFX音量
@@ -305,6 +389,15 @@ public abstract class AbstractAudioManagerSystem : AbstractSystem, IAudioManager
{
SfxVolume = volume;
}
+
+ ///
+ /// 获取SFX音量
+ ///
+ /// 音量大小,范围0-1
+ public virtual float GetSfxVolume()
+ {
+ return SfxVolume;
+ }
///
/// 设置语音音量
@@ -314,6 +407,15 @@ public abstract class AbstractAudioManagerSystem : AbstractSystem, IAudioManager
{
VoiceVolume = volume;
}
+
+ ///
+ /// 获取语音音量
+ ///
+ /// 音量大小,范围0-1
+ public virtual float GetVoiceVolume()
+ {
+ return VoiceVolume;
+ }
///
/// 设置环境音量
@@ -323,6 +425,15 @@ public abstract class AbstractAudioManagerSystem : AbstractSystem, IAudioManager
{
AmbientVolume = volume;
}
+
+ ///
+ /// 获取环境音量
+ ///
+ /// 音量大小,范围0-1
+ public virtual float GetAmbientVolume()
+ {
+ return AmbientVolume;
+ }
///
/// 检查背景音乐是否正在播放
diff --git a/GFramework.Core.Godot/system/IAudioManagerSystem.cs b/GFramework.Core.Godot/system/IAudioManagerSystem.cs
index 47cf1f1..483479b 100644
--- a/GFramework.Core.Godot/system/IAudioManagerSystem.cs
+++ b/GFramework.Core.Godot/system/IAudioManagerSystem.cs
@@ -24,6 +24,30 @@ public interface IAudioManagerSystem : ISystem
/// 音调调整
void PlaySound(string audioPath, float volume = 1.0f, float pitch = 1.0f);
+ ///
+ /// 播放特效音效
+ ///
+ /// 音频文件路径
+ /// 音量大小,范围0-1
+ /// 音调调整
+ void PlaySfx(string audioPath, float volume = 1.0f, float pitch = 1.0f);
+
+ ///
+ /// 播放语音
+ ///
+ /// 音频文件路径
+ /// 音量大小,范围0-1
+ /// 音调调整
+ void PlayVoice(string audioPath, float volume = 1.0f, float pitch = 1.0f);
+
+ ///
+ /// 播放环境音效
+ ///
+ /// 音频文件路径
+ /// 音量大小,范围0-1
+ /// 音调调整
+ void PlayAmbient(string audioPath, float volume = 1.0f, float pitch = 1.0f);
+
///
/// 停止背景音乐
///
@@ -45,18 +69,72 @@ public interface IAudioManagerSystem : ISystem
/// 音量大小,范围0-1
void SetMusicVolume(float volume);
+ ///
+ /// 获取背景音乐音量
+ ///
+ /// 音量大小,范围0-1
+ float GetMusicVolume();
+
///
/// 设置音效音量
///
/// 音量大小,范围0-1
void SetSoundVolume(float volume);
+ ///
+ /// 获取音效音量
+ ///
+ /// 音量大小,范围0-1
+ float GetSoundVolume();
+
+ ///
+ /// 设置特效音量
+ ///
+ /// 音量大小,范围0-1
+ void SetSfxVolume(float volume);
+
+ ///
+ /// 获取特效音量
+ ///
+ /// 音量大小,范围0-1
+ float GetSfxVolume();
+
+ ///
+ /// 设置语音音量
+ ///
+ /// 音量大小,范围0-1
+ void SetVoiceVolume(float volume);
+
+ ///
+ /// 获取语音音量
+ ///
+ /// 音量大小,范围0-1
+ float GetVoiceVolume();
+
+ ///
+ /// 设置环境音量
+ ///
+ /// 音量大小,范围0-1
+ void SetAmbientVolume(float volume);
+
+ ///
+ /// 获取环境音量
+ ///
+ /// 音量大小,范围0-1
+ float GetAmbientVolume();
+
///
/// 设置主音量
///
/// 音量大小,范围0-1
void SetMasterVolume(float volume);
+ ///
+ /// 获取主音量
+ ///
+ /// 音量大小,范围0-1
+ float GetMasterVolume();
+
///
/// 检查背景音乐是否正在播放
///