How to stop sounds played with "Notify - PlaySound" in AnimInstance ?

Hi everyone,

I’m currently working on animations.

I have some “hard coded” animation among a dialog sound (since I’m not using FaceFX). So when an animation starts, I play the dialog sound using a “Notify - PlaySound”. This works pretty much as I want to.

But how can I stop the sound when the animation is cut off before the end ? For now, the sound will just continue to play even if the animation is stopped.

What I found so far:

  • Use a custom C++ AnimInstance class which store all the possible sounds for my animations but it’s dirty as f*** and really not convenient.
  • Create a custom AnimNotifyState and overriding Received_NotifyBegin & Received_NotifyBegin for each animation which is better but still not very convenient.
  • Use a Matinee for each animations

What do you guys think about that ?

Thanks for reading. Best regards.

1 Like

All right so finally I came with this solution. Just posting it if anyone is looking for the same thing :).

I’ve made it in C++ but you can do it in blueprints as well

  1. First I created a child class of UAnimNotifyState and add some sounds property to it

    UCLASS()
    class POLICE_API UPOLICE_AnimNotifyState : public UAnimNotifyState
    {
    GENERATED_BODY()

    public:

     // The animation is starting
     virtual void NotifyBegin(USkeletalMeshComponent* MeshComp,
     	UAnimSequenceBase* Animation,
     	float TotalDuration) override;
    
     // The animation stoped
     virtual void NotifyEnd(USkeletalMeshComponent* MeshComp,
     	UAnimSequenceBase* Animation) override;
    

    public:

     // Sound to Play
     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "POLICEAnimation")
     USoundBase* m_SoundToPlay;
    
     // The audio player
     UPROPERTY(BlueprintReadWrite, Category = "POLICEAnimation")
     UAudioComponent* m_AudioComponent;
    

    };


    ///////////////////////////////////////////////////////////////////////////////
    void UPOLICE_AnimNotifyState::NotifyBegin(USkeletalMeshComponent* MeshComp,
    UAnimSequenceBase* Animation,
    float TotalDuration)
    {
    // Spawn the sound
    if (m_SoundToPlay)
    {
    m_AudioComponent = UGameplayStatics::SpawnSoundAttached(
    m_SoundToPlay, MeshComp, NAME_None,
    FVector(ForceInit), EAttachLocation::KeepRelativeOffset,
    true
    );
    }
    }

    ///////////////////////////////////////////////////////////////////////////////
    void UPOLICE_AnimNotifyState::NotifyEnd(USkeletalMeshComponent* MeshComp,
    UAnimSequenceBase* Animation)
    {
    // Stop the sound if needed
    if (m_AudioComponent)
    {
    m_AudioComponent->Stop();
    m_AudioComponent = NULL;
    }
    }

  2. Add the custom notify UPOLICE_AnimNotifyState in the animation notifies section and set its duration to the whole animation duration (so the notify will be alive during the whole animation).

  3. Select the sound you want to play in the details panel.

Results:

The sound will starts when the animation starts, and stop whenever the animation is stoped.
If you have any feedback I would be glad to hear them !

Possible improve

Right now, if the animation is paused (for example in the animation blueprint editor), the sound will stop, but not resume where is was before.

Cheers! Good luck, have fun <3

1 Like

Hello, how can you do this with blueprint? I tried to reproduce it, but seems not possible to get the “audio component” and stop it, can you please explain it a little bit? Thank you.

As you can see in my header file I’ve manually add an UAudioComponent and initialize it at runtime with UGameplayStatics::SpawnSoundAttached.
You can do this in blueprints to. Create a blueprint class inheriting from AnimNotifyState and add an Audio component in the components section. Then you should be able to do the exact same thing I did with C++

Good luck :slight_smile:

Yeah, I already did that, but how can you know when to stop it? Also, how to stop it with the “stop” node?

*Regarding on how the resume the sound, you just need to split the audio file, so when the animation is paused you cut the audio at that moment and when you resume it play the next audio file you splitted.

how can you know when to stop it ?

In C++ the YOURAnimNotifyState::NotifyEnd is automatically called when your notify ends (the notify you add in your animation) so you be able to override the same kind of event in your blueprint.

Also, how to stop it with the “stop” node?

The answer is in the question :D. Just use the stop node using the reference returned from SpawnSoundAttached

I don’t know about pausing and I didn’t explore this issue yet.

Sorry to bother you, but in notify blueprint you can’t set variables, can’t call functions within it, can’t create custom events, it’s very limited, how can you reference that SpawnSoundAttached in NotifyEnd?
Did you tried this in blueprint?
Can you post an image of what to be done?

Thanks for your time and patience with me :slight_smile:

All right let’s do this step by step:

  1. Create a blueprint inheriting from AnimNotifyState
  2. Create a variable of type Audio Component Reference and make it editable
  3. Override the Recieved Notify Begin and Recieved Notify End function. In theses functions, you will be able to play and stop the AudioComponentReference you just created
  4. In your animation, add a notify and select the notify type you just created at 1.
  5. You should be able to select the sound to play in the animation directly ;). It should be the same thing we can see in my answer’s screenshot

Example:

However, like I said before, you won’t be able to pause the sound when the animation is paused.

I don’t know if it’s me that don’t understand, but with AudioComponent you can’t set a sound, you need a SoundBase reference to set a sound, also AudioComponent is empty, it doesn’t work…

Yeah sorry, you are right I mixed both. As you can see in my C++ header I have two member:

  1. An USoundBase to hold the sound reference to play (you will be able to set the sound here)
  2. An UAudioComponent to handle the player (so you will be able to play and stop the previous sound)

In Recieved Notify Begin, just do as you did in your previous comment, use SpawnSoundAttached and save the Audio Component Reference you get here. (But if I remember well, you may not be able to change the variable in the notify blueprint, which sucks)

In Recieved Notify End just call stop on your Audio Component Reference

If you can modify variable in the notify blueprint, just go for the C++ solution since it’s working (I used it already). Sometimes, you just can’t do what you need in blueprints.

I found a solution for blueprint :slight_smile:

  1. Add an AudioComponent in the character blueprint of the mesh you want to animate.
  2. Create a blueprint inheriting from AnimNotifyState
  3. Override the RecievedNotifyBegin and RecievedNotifyEnd
  4. In NotifyBegin call the AudioComponent you added in the character blueprint and do this:

  1. In NotifyEnd do this:

Now call this NotifyState in the animation you want to stop the sound, select the sound you want to play and you are done.

You can call this notify in any animation of that character you want to stop the sound at a certain point :slight_smile:

1 Like

Nice one :wink:

Just came across this, and it’s exactly what I needed. Thanks!

Hey guys. Can you tell me what I am doing wrong? I keep going over the what y’all said and it looks identical doesn’t it? I created a new AnimNotify blueprint > clicked override in the functions and hit notify begin and end > created an audio component variable in my character blueprint > created a sound base variable in the AnimNotify blueprint > copied what Ross90 has and added the notify state to the character animation. But I keep getting this error. It is driving me up the wall.






Holy sh*t. This is a 7 year old post. Ok well it is the only thing I have seen on the internet exactly to what I want so hopefully someone knows what is being said here and can help me out

1 Like