How to change a sound class’s volume in C++? I’m trying to build an audio options menu, where you can configure the Master (overall) volume, Effects volume, Music volume, Voice volume, etc.
I tried changing it via GEngine->GetAudioDevice()->SetClassVolume(SoundClass, Volume);, but nothing happens. Below is the full code.
I debugged the code and it was finding the sound classes (Master, SFX, etc.), and it was assigning the new values, but the volume didn’t actually change.
Also make sure there is only one “Master” sound class.
That was the problem! There were 3 ‘Master’ sound classes registered on the device, for some reason. Hmm ahh: one from the Engine, one from Game/Sounds (maybe from ShooterGame), and one from Game/ExampleContent (from the examples project).
I’d like to suggest a small improvement I did on the function. As the finding class by name part uses FString::Find it could get the wrong class searching for a name that’s part of another class. For example, it could give you SFX_Weapons while searching for SFX, as SFX_Weapons also contains SFX. So, instead, I look for the part after the . in the full name. The names are like *Engine/Something/Sound.Master * , so the class name we look for is right after the only . in the name. Splitting the name by the . and getting the right part, we have the class name only.
Code (tested on 4.10.4)
#include "AudioDevice.h"
void SetSoundClassVolume(FString ClassName, float Volume, bool &Success)
{
FAudioDevice* AudioDevice = GEngine->GetMainAudioDevice();
Success = false;
if (!AudioDevice) return;
for (auto i = AudioDevice->SoundClasses.CreateIterator(); i; ++i)
{
USoundClass* SoundClass = i.Key();
FString SoundClassName;
// Test if the Split function works then, if the name was the right one
if (SoundClass->GetFullName().Split(L".", nullptr, &SoundClassName, ESearchCase::CaseSensitive)
&& SoundClassName.Equals(ClassName))
{
SoundClass->Properties.Volume = Volume;
Success = true;
return;
}
}
}
In the newer version of Unreal Engine FAudioDevice::SoundClasses has been made private. Instead the default USoundClass can be easily accessed from UAudioSettings or if the access of a custom USoundClass is desired it can be accessed by calling LoadObject<>().
// Change volume of SoundClass by specifying asset path to SoundClass
ChangeVolume(float volume, FSoftObjectPath soundClassPath) {
// Guard statement against invalid FSoftObjectPath
if (!soundClassPath.IsValid())
{
return;
}
// Load object if not already loaded
USoundClass* soundClass = LoadObject<USoundClass>(nullptr, *soundClassPath.ToString());
// Guard against unsuccessful object instantiation
if (soundClass == nullptr) {
return;
}
//
soundClass->Properties.Volume = volume;
}
// Change the volume of the default SoundClass
ChangeDefaultVolume(float volume) {
const FSoftObjectPath DefaultSoundClassName = GetDefault<UAudioSettings>()->DefaultSoundClassName;
if (!DefaultSoundClassName.IsValid())
{
return;
}
USoundClass* masterSoundClass = LoadObject<USoundClass>(nullptr, *DefaultSoundClassName.ToString());
if (masterSoundClass == nullptr) {
return;
}
masterSoundClass->Properties.Volume = volume;
}