How to make start and stop music by clicking on the same button?

Hello, I have UE 5.4.1

After 3 day on diving into YouTuBe tutorials and UE documentation I´m lost with creating such simple thing as strat and stop music function by clicking on same button in UI blueprint.

I´ve created UI with ONE button that I need to play music after first click and stop after second one.
I imported sound wav and created cue from it that I need to play. It sould be global sound that should play while traveling through the app.

I´ve tried to connect my OnClicked button on FlipFlop function > A node is for Start 2D Sound (selected sound cue) and B node to Stop audio component. Also, I´ve created variable Audio Component inside the UI blueprint.

Problem is that I can not find my sound cue or wav in Audio Componentz > Details > Sound. I have bot files in my content browser but engine does not see them.

Question:

  1. Where should I put these files to reveal them into the Sound > Details menu?
  2. Could someone advide me some another workaround, please?

Hey @Alwin43535! Welcome to the forums!

Try this!

Let me know how it goes! :slight_smile:

hello sir I want to know what’s the meaning of the “Radio” of your blueprint. Is it as a virable?

Hey @HelloMHL, it is a sound object reference variable, you can make sure the type is correct by using “Promote to Variable” on the “Return value” of the “Spawn Sound 2D”. Then build the rest of it using that variable!

Hope this helps!

Hi @Alwin43535 !

Hope all is well.

The reason your node is unable to stop the sound cue you triggered is because it is unable to find/reference it. This can happen when a sound is triggered with a “Play Sound 2D” node which is more suited for “Fire-and-forget” usages. Also, try setting up the Audio variable’s type as a “Sound Base” object reference. This will help you find it in the content browser:

Regarding the triggering logic, there are a couple ways to accomplish the effect you’re looking for:

1.- Assuming a blueprint and not a widget, one method would involve creating an audio component inside that blueprint, setting its content to the Sound Cue containing the music track, disabling the auto-play checkbox, and then using the audio component’s “Play” and “Stop” nodes to control the track’s playback state.

2.- In the event of it being a widget, another way would also involve spawning the music track with a “Create Sound 2D” node using the “onInitialized” event (so that it calls it upon creating the widget). This will return a reference to the audio instance. Once you have access to that reference, you can then stop/play as needed.

3.- You may also create a “Sound Base” local variable object reference within the widget, set the correct music track within, and then spawn + control using “Create Sound 2D” or “Spawn Sound 2D” nodes which will both return a reference to the sound instance! Please note that Spawn Sound 2D will auto-play the audio being spawned.

Best of luck!