Play a sound when key is pressed, stop when no longer pressing.

Hello all

Trying to implement the idea of the thread title using Blueprint, which actually seems a little un-intuitive to me at the moment.

The idea is to set up a Blueprint where you can specify both the sound (that loops) and the input key, in the construction script area. Then in the event graph I am trying to set things up so that when you press the key, the sound is played continuously until you let go, when it will stop.

This does not seem straightforward with what is available. Any tips on how to complete this seemingly simple task? The audio capabilities don’t seem to have a way to “stop” a sound that is playing.

Full disclosure, I’m definitely not an expert on audio through blueprints, but I believe one way you could do this would be to add an audio component to the actor blueprint you want to do this in and then use the “SetSound,” “Play,” and “Stop” blueprint nodes available off of the component.

For a quick experiment, I just set the sound on the component in BeginPlay (but you could do this in the properties window of the component if it’s just going to be one sound that doesn’t change), and then used Play and Stop when the right-mouse button is pressed and released, respectively.

2 Likes

Hey Billy, thanks for the incredibly fast response. This seems to make complete sense and will give it a shot. Funny that I would greatly prefer code in this case, but we are starting with Blueprints to make it more accessible to everyone. But I was dying to do a simple audio.play()/stop()!

This also helps with some insight on how Blueprints work. Thanks again!

Edit: Didn’t realize how many functions using “context sensitive” was removing from the view. Play and stop are clearly right there.

No problem at all! Let me know how it goes. Hopefully Play and Stop should show up in the context sensitive option too (as long as you’re pulling off of an audio component), but I think they end up being in an awkward spot in this case, because you have to expand “Call Function” -> “Audio” -> “Components.” I totally initially missed the “Components” piece being expandable when I went to get that screenshot for my last post because there just happen to be two audio related functions that aren’t categorized that show up in the list as well. Will ask around if we can make that a little clearer.

Yep, this is correct. Also, if you want to have more gentle transitions, you can use the Fade In function to play the sound with a defined linear fade-in time, and the Fade Out function, which will execute a linear fade-out of the audio over a specified time and then stop.
Best-
Zak

What IF i need to :
Keypress active - play startsound - then play looped sound until i press the deactivate key then play endsound…

What im trying to accomplish is to add a “turn on tap water” with emitters when pressing ‘enter’ the water will pour and as long as i want and later when i press “enter” again the water would stop.

I tried different ways but i being new at this my logic is probably wrong when put down in blueprint…:-/

EDIT: I’ve found out that my W event isnt even firing, why is that?

Hey there,

At the moment I am trying the same thing as the started of this thread.
I am currently running into a problem with footstep sounds. Our game has no character and is in first person.
I want a soundcue to play whenever W,A,S or D is pressed and I want to stop it when W,A,S or D is released.
I created a blueprint using the soundcue and created the following blueprint:
Soundcue BP:


Soundcue options:


Could you help me out?

Add the sound cue as an audio component on the character itself. Because you aren’t using animation, prototype something along these lines.

Event Tick -> Branch. For the Bool do an AND operation (isMovingOnGround AND Acceleration.Length > 0), for the True output put your Play, for the False put your Stop.

This is not a final solution, if this functions the way I think it will the timing will be way off because it will fire every Tick as long as you are on the ground and inputting WASD.

If your initial idea ended up working the same thing would occur, as the W key would be sending inputs every tick. You will need some way to delay firing the sound every tick

Was just looking for this tutorial! Thanks! :slight_smile:

Two years from now, but I’m really going nuts (how, do I add your ‘MyAudioComponent’, I just can’t find it)

Turn off context sensitivity, also the key wont be working cause it’s not the pawn/controller class

Pretty sure you create a new Var in the left column and the choose Audio under the component tab

Hey there,

At the moment I am trying the same thing as the started of this thread.
I am currently running into a problem with footstep sounds. Our game has no character and is in first person.
I want a soundcue to play whenever W,A,S or D is pressed and I want to stop it when W,A,S or D is released.
I created a blueprint using the soundcue and created the following blueprint:
Soundcue BP:

Hello I want do the same and how can I add W event in Evengraph with Pressed and Released executive

can you help me?

I did wall you said but still it’s not working