How to set a button to pause background music

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By bbacle

I have a project with a background audiostream playing, & I would like to add the feature where a player can pause the music & then start it back up at the point it was paused (not from the beginning).
How would I do that?
I have tried selecting stream paused in the Inspector, but all that does is stop the music from playing at all.
I would like to have it where a player can tap a button on the computer keyboard to pause the music.
Can someone show me how that is to be done - please step by step as I am new to Godot?
Thank you.

:bust_in_silhouette: Reply From: exuin

Hi, again!

First, selecting the “Stream Paused” option in the inspector will stop the music because… well, the stream is paused. The values in the inspector are the initial values for the node and will not change.

Anyway, I’ll just go through this step by step.

  1. Click on “Project”, “Project Settings”, and then “Input Map”. Add a new action by typing in a name (like “pause”) and clicking the “Add” button.

  2. Click the “+” next to the action and then select the option that you want to correspond to the action. Since you want the player to hit a key on their keyboard, select “Key” and hit the key on the keyboard that you choose. Click “OK”.

  3. In the scene docker, click on your AudioStreamPlayer. Press the scroll with the green plus sign to add a script to the player.

  4. Replace the auto-created script with this:

extends AudioStreamPlayer

func _input(event):
	if event.is_action_pressed("pause"):
		stream_paused = !stream_paused

That’s all the steps! You do not have to follow these steps exactly, and I encourage you to look up more information about the nodes you’re using so that you can understand this better. If you need more help, I can send you an example project.

I copied & pasted the code you put in the email, & I tried manipulating it every which way, but it wouldn’t work.
I finally just copied & pasted the code you have here & it worked perfectly.
Thanks for the help.

bbacle | 2021-01-11 19:58

Yeah sorry I edited the comment afterwards

exuin | 2021-01-11 20:08

Thank you exuin, It worked for me.

susnomics | 2021-02-16 15:57