【UE4 C++】播放声音与控制音量

66 篇文章 7 订阅

播放声音

  • 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);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值