How to change sound volume in C++?

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.

void FShooterOptions::ChangeVolume(FString ClassName, float Volume)
{
	FAudioDevice* Device = GEngine->GetAudioDevice();
	if (!Device)
	{
		return;
	}
	
	USoundClass* SoundClass = NULL;
	for (TMap<USoundClass*, FSoundClassProperties>::TIterator It(Device->SoundClasses); It; ++It)
	{
		USoundClass* ThisSoundClass = It.Key();
		if (ThisSoundClass && ThisSoundClass->GetFullName().Find(ClassName) != INDEX_NONE)
		{
			SoundClass = ThisSoundClass;
			break;
		}
	}

	if (SoundClass)
	{
		Device->SetClassVolume(SoundClass, Volume);
	}
}

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.

I’ve managed to achieve that by changing sound class volume property. Instead of

Device->SetClassVolume(SoundClass, Volume);

try

SoundClass->Properties.Volume = NewMasterSoundVolume;

Also make sure there is only one “Master” sound class.

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).

So the solution is:

void FShooterOptions::ChangeVolume(FString ClassName, float Volume)
{
	FAudioDevice* Device = GEngine->GetAudioDevice();
	if (!Device)
	{
		return;
	}
	
	for (TMap<USoundClass*, FSoundClassProperties>::TIterator It(Device->SoundClasses); It; ++It)
	{
		USoundClass* SoundClass = It.Key();
		if (SoundClass && SoundClass->GetFullName().Find(ClassName) != INDEX_NONE)
		{
			Device->SetClassVolume(SoundClass, Volume);
		}
	}
}

Thanks!

Thanks for posting this code - was a great time saver!

Since 4.7 it’s

SoundClass->Properties.Volume = Volume;

instead of

Device->SetClassVolume(SoundClass, Volume);

Small improvement

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;
}
1 Like

@Dantali0n Do you have any idea how to set the volume of a media player?