Audio Stream Player Not Being Able To Play in GDScript?

Godot Version

4.6

Question

Hello!! Im trying to put some music at a specific point in one of my scenes, but when i tried importing it as an mp3, i kept running into problems of it not starting in gdscript.

It’ll only start if i have it set on autoplay, but i need to delay it a little bit, so i was going to try and play it in gdscript. When i had the code at the bottom, though (which im very unsure about, but a few tutorials I saw said to use it, along with hovering over the ‘play’ inspector thing itself), it just wont play. I also tried importing it as a wav file to see if that was, on the off chance, the problem, but it gave me so many different errors (that all amounted to “can not import/read this”) that i couldn’t post them here without it being unreadable.

Any thoughts?

func ready():
	stream_paused = true
	if Globals.song != false:
		stream_paused = false
		play()

I don’t think you need stream_paused for anything at all, you can just remove those.
Also try to print Globals.song

Also side note, doing Globals.song != false is very hard to read and it’s super redundant. You can just do if Global.song:

1 Like

huh, okay, thank you!! i didnt know variables worked like that (time to go fix all the other ones i had haha)

but its still not playing, nor is it printing anything.

extends AudioStreamPlayer2D

func ready():
	if Globals.daisybell:
		play()
		print("daisybell")

That means the if statement is never true in the ready function. The ready function only runs a single time so even if you change that variable in the future, it’ll never be executed again.

1 Like

ready is not a function override, _ready() will be automatically called when the node is added to the scene. You probably meant to use _ready

1 Like

okay, thank you :smiley: now its printed 164 “daisybell”s, but the song isnt starting.


func _process(delta: float) -> void:
	if Globals.daisybell:
		play()
		print("daisybell")

play() will restart the audio stream, by using it in _process you are restarting the song every frame. You could check if not playing: play(), but that sounds kind of wild. Maybe you should find a way to reference this audio stream player and play it through your global script?

1 Like

okay, so ive tried that now, and its giving me a new error (which im taking as a good sign, hopefully?)

var playing1 = load("res://Art/daisy_bell.mp3")

Thats my global variable, and its reading it fine.

but the problem comes in the script for my audiostreamplayer:

func _process(delta: float) -> void:
	if Globals.daisybell:
		print("daisybell")
		Globals.playing1.play()

and it gives me the error “Invalid call. Nonexistent function ‘play’ in base ‘AudioStreamMP3’.” Which it wasnt before?

So this is only a mp3 file, not an audio stream player

If this was an audio stream player you would run into the same issue as before, because this code is still in _process and still re-starting what would be an audio player every frame.


What I’d recommend is giving your Global an AudioStreamPlayer node, then using a new function to play “daisy_bell” instead of using a single variable and checking if it’s set.

# global.gd
extends Node

# Make sure to add an AudioStreamPlayer as a child. This example expects the name "MusicPlayer"
@onready var music_player: AudioStreamPlayer = $MusicPlayer

func play_daisy_bell() -> void:
    music_player.stream = load("res://Art/daisy_bell.mp3")
    music_player.play()

I don’t know how your game code works, but instead of setting Global.daisybell = true I believe you can swap it out for Global.play_daisy_bell()

2 Likes

thank you!! I got it working (i connected it to the main node instead of globals though; i have dialogue in the scene, so im now using that to call the diasybell function). Thank you so much!!!