0 votes

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://godotengine.org/qa/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

in Engine by (22 points)
edited by

1 Answer

+1 vote

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

by (263 points)

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: https://prnt.sc/sek2ta

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?

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

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()

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?

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.