How to solve error "no conversion from ‘nullptr’ to FAudioDeviceHandle"?

Hi,

I have a project in VS 2022, UE5.1, and I am trying to get Main Audio Device to apply selected volume settings:

FAudioDevice* MainAudioDevice = (GEngine ? GEngine->GetMainAudioDevice() : nullptr);

if (MainAudioDevice)

{
	MainAudioDevice->SetTransientPrimaryVolume(SoundVolume);
}

but I am getting error C2446:
“no conversion from ‘nullptr’ to FAudioDeviceHandle’”

Any advice on how to proper reference this property would be appreciated.

Thanks

// needs the following includes
#include "AudioDevice.h"
#include "AudioDeviceManager.h" 

void YOURCLASS::DoSomething() {
	FAudioDevice* MainAudioDevice = nullptr;
	FAudioDeviceHandle handle = GetWorld()->GetAudioDevice();					
	MainAudioDevice = handle.GetAudioDevice();
	if (MainAudioDevice)
	{
		MainAudioDevice->SetTransientPrimaryVolume(SoundVolume);
	}	
}
2 Likes

Hi,

GEngine::GetMainAudioDevice() returns a FAudioDeviceHandle struct, not a FAudioDevice pointer. You should be able to access the underlying FAudioDevice* with FAudioDeviceHandle::GetAudioDevice()

Keep in mind that some engine resources may not be available on Dedicated Servers, Audio Devices among them. This post may be able to help you if this is the case.

1 Like

Thank you so much for the help. I’ll give it a try and let you know if that works.

Hi there,

Thank you for sharing the link and for the assistance. I’m considering a single-player game at this moment. I’ll check both solutions and let you guys know if that works.

Thank you, guys for the help!
Keep up the good work.

@3dRaven
Hi there, I was able to compile, but now the project crashes during start up while executing this line:

FAudioDeviceHandle handle = GetWorld()->GetAudioDevice();

The error message is:
Exception thrown at 0x00007FFEC3BA53C0 (UnrealEditor-Engine.dll) in UnrealEditor.exe: 0xC0000005: Access violation reading location 0x00000000000001D8.

Perhaps I need to reference that the user is already in editor or something like that.
Can you please help me with this?
Thanks

The function runs no problem on my end. I’m guessing the error comes from the context in which you are calling the function.

  • Is it during gameplay
  • is it being called in the constructor / CDO
  • is it being called from an editor widget

For the function to work it has to be called after the begin play phase. The world needs to exist for it to “grab it” from the actor context.

Calling it anytime sooner will cause a problem as the GetWorld will fail.

Added some fail-safes to make it compile (it may try to get world during compile run) and pushed the call to postInitializeComponents to play at start if needed (world context is then valid)

Tested with audio playback and it adjusts the volume as expected.
Here is a full dump of the test class I use

header

#pragma once

#include "CoreMinimal.h"
#include "Test.generated.h"

UCLASS()
class CUSTOMIMPORT_API ATest : public AActor
{
	GENERATED_BODY()

public:
	UFUNCTION(BlueprintCallable)
		void DoSomething();

	ATest();
	UPROPERTY(EditAnywhere,BlueprintReadWrite)
	float SoundVolume = 1;

	virtual void PostInitializeComponents() override;

};

cpp

#include "Test.h"
#include "AudioDevice.h"
#include "AudioDeviceManager.h" 

ATest::ATest() {
	
}

void ATest::PostInitializeComponents() {
	Super::PostInitializeComponents();
	DoSomething();
}

void ATest::DoSomething() {
	FAudioDevice* MainAudioDevice = nullptr;
	if (GetWorld() != nullptr) {
		FAudioDeviceHandle handle = GetWorld()->GetAudioDevice();
		if (handle.IsValid()) {
			MainAudioDevice = handle.GetAudioDevice();
			if (MainAudioDevice)
			{
				MainAudioDevice->SetTransientPrimaryVolume(SoundVolume);
			}
		}
	}
}

Thank you so much for your elaborated answer. That helped me to better understand the logic and the code structure. That quoted part fixed the crash, and now the game settings are correct. I’ll close this thread with your solution.

Thank you again.

1 Like