[4.18] How to change volume of MediaPlayer playback?

I found out my approach overcomplicated. Instead of using dynamically created USoundClass I can directly change audio volume in UMediaSoundComponent by accessing it’s UAudioComponent property and then using AdjustVolume method:

UAudioComponent * audioComponent = SoundComponent->GetAudioComponent();
audioComponent->AdjustVolume( 0.0f, volume );

GetAudiocomponent requires you to add “AudioMixer” module in your .Build.cs file.

Appendix:

If somone would wonder how to dynamically create USoundClass and use it with UMediaSoundComponent I got it working. The key is to restart UAudioComponent obtained from MediaSound by using Stop and Play methods after setting it’s SoundClassOverride property. There is also important note from @Minus_Kelvin about getting proper FAudioDevice from object’s UWorld context.

MediaPlayer = NewObject< UMediaPlayer >( this );
SoundClass	= NewObject< USoundClass >( this );
SoundMix	= NewObject< USoundMix >( this );


UWorld * world = MediaSoundComponent->GetWorld();
FAudioDevice * audioDevice = world->GetAudioDevice();

UAudioComponent * audioComponent = MediaSoundComponent->GetAudioComponent();

if( audioDevice && audioComponent )
{
	audioDevice->RegisterSoundClass( SoundClass );
	audioDevice->PushSoundMixModifier( SoundMix );

	audioComponent->SoundClassOverride = SoundClass;
	audioComponent->Stop();
	audioComponent->Play();
}

...

audioDevice->SetSoundMixClassOverride( SoundMix, SoundClass, volume, 1.0f, 0.0f, false );
2 Likes