【UE4 C++】播放声音与控制音量
播放声音
-
PlaySoundAtLocation()
USoundCue* HitSound = LoadObject<USoundCue>(this, TEXT("SoundCue'/Game/Demo_Drone/Sound/explore_Cue.explore_Cue'")); if (HitSound) { UGameplayStatics::PlaySoundAtLocation(this, HitSound,SweepResult.Location); }
-
PlaySound2D()
UGameplayStatics::PlaySound2D(GetWorld(), HitSound)
-
使用组件
- static UAudioComponent* SpawnSound2D()
- static UAudioComponent* CreateSound2D()
- static UAudioComponent* SpawnSoundAtLocation()
- static UAudioComponent* SpawnSoundAttached()
-
slate 中播放声音
FSlateApplication::Get().PlaySound(MenuStyle->MenuBGM);
控制音量
-
加载USoundCue资源 直接改变 USoundCue::VolumeMultiplier
FSlateSound 资源亦可以强转
USoundCue* sound= Cast<USoundCue>(MenuStyle->StartGameSound.GetResourceObject()); sound->VolumeMultiplier = SoundVolume;
-
通过 FAudioDevice、USoundMix、USoundClass
- Conten 下创建 USoundClass 、USoundMix资源
- USoundMix资源属性中添加 Sound Class Effects 元素,元素为刚创建的USoundClass 资源
- Sound Wave 资源属性中设置 Sound Class 为刚创建的 USoundClass
- C++ 实现
// .h定义 class USoundMix* SoundMix; class USoundClass* MusicClass; class USoundClass* SoundClass; class FAudioDevice* AudioDevice; //.cpp 调用 初始化 MusicClass = LoadObject<USoundClass>(nullptr,TEXT("SoundClass'/Game/Blueprints/Sound/MusicClass.MusicClass'")); SoundClass = LoadObject<USoundClass>(nullptr,TEXT("SoundClass'/Game/Blueprints/Sound/SoundClass.SoundClass'")); SoundMix = LoadObject<USoundMix>(nullptr,TEXT("SoundMix'/Game/Blueprints/Sound/SoundMix.SoundMix'")); AudioDevice = GEngine->GetMainAudioDeviceRaw(); AudioDevice->PushSoundMixModifier(SoundMix); //.cpp 调用 改变音量 AudioDevice->SetSoundMixClassOverride(SoundMix, MusicClass, MusicVolume, 1.f, 0.2f, false); AudioDevice->SetSoundMixClassOverride(SoundMix, SoundClass, SoundVolume, 1.f, 0.2f, false);
作者:砥才人
出处:https://www.cnblogs.com/shiroe
本系列文章为笔者整理原创,只发表在博客园上,欢迎分享本文链接,如需转载,请注明出处!