Mute Button - How to pause the music in game background?

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

Hello everyone. I’m new here, only have been a couple of days learning Godot.

I would like to know, how to make a functional mute button, to pause the music in the game background.

What are the required steps and scripts?
So when I click the button (the music is off), when I click the button again (the music is on).

I previously have read this question article below, by Marko:
https://forum.godotengine.org/60657/pause-music-background-checkbutton-need-script-please-help

But I still couldn’t understand the process.

So far, I’ve only put the Music button sprite using TextureButton in the main menu scene. While the AudioStreamPlayer on audio scene.

Note: menu & audio are 2 different scenes in 2 different folders.

If someone could write a specific explanation in detail, step-by-step on how to do it, that would be appreciated.

~ Regards

:bust_in_silhouette: Reply From: Eidam

Hello,
my scene:

Node2D
-Button
-AudioStreamPlayer2D

And this is the code attached to the node Node2D:

extends Node2D

func _ready():
	$"AudioStreamPlayer2D".play()

func _on_Button_pressed():
	$"AudioStreamPlayer2D".stream_paused = !$"AudioStreamPlayer2D".stream_paused

If you need more help, write

I’ve followed your steps.

The current scene:
audio_music (Node2D)
- button (Button)
- music (AudioStreamPlayer2D)

The current GDScript codes attached to the node audio_music:

extends Node2D

func _ready():
   $music.play()

func _on_button_pressed():
   $music.stream_paused = !$music.stream_paused

But the result remains the same. Whenever I press the button of the music sprite, the music continues. Picture link: Screenshot by Lightshot

Also my original idea for the mute button is this:

My scenes:

menu

CanvasLayer
- btn_play (TextureButton)
- btn_credits (TextureButton)
- btn_quit (TextureButton)
- btn_mute (TextureButton)

audio_player

Node
- ResourcePreloader
- AudioStreamPlayer

Is there a way for the “btn_mute” in scene menu can mute the music in the entire game background, while the “AudioStreamPlayer” is in scene audio_player?

If so, then what are the steps to do that? as well as the required scripts?

Plaz_GD | 2020-05-11 08:36

Hello,
try instead of TexureButton nodes use alone Button with icon. When I add TextureButton, I also can´t pause music… With Button only with icons works it!

Here is my scene: Picture

And here is the code:

extends Node2D

var icon = 1

func _ready():
	$"AudioStreamPlayer".play()

func _on_Button_pressed():
	$"AudioStreamPlayer".stream_paused = !$"AudioStreamPlayer".stream_paused
	if icon == 1:
		$"Button1".icon = load("res://stop.png")
		icon = 2
	else:
		$"Button1".icon = load("res://play.png")
		icon = 1

This is how it work: Video-without audio

Eidam | 2020-05-11 09:14

And if you need play/stop music from another scene, do this:

  1. Add scene with AudioPlayer to Autoload (Project-> Settings-> Autoload-> Open scene with AudioPlayer, name it (something like Music) and allow Singleton
  2. In other scene´s script write this:
Name_of_Singleton.get_node("AudioStreamPlayer").play()

Eidam | 2020-05-11 09:24

Regarding your scene in the link below:
https://ctrlv.cz/shots/2020/05/11/nMbz.png

I noticed that you also add scripts to the button node, as well as giving it a signal.

Is the script for the button node the same as the Music (Node2D)?

And what signals should be given to the button node?

Plaz_GD | 2020-05-12 01:12

Hello,
Script added to button is empty, I had this for other things… And I have one signal from Button to script Node2D: func _on_Button_pressed():

Eidam | 2020-05-12 08:31

Question.
Does the “mute button” have to be set to toggle? And the signal to the on toggled signal?

Or does the code
$AudioStreamPlayer2D.stream_paused = !$AudioStreamPlayer2D.stream_paused

essentially turn it into a toggle?