So, if I'm understanding your intentions correctly, I think this script will do what you're looking for:
extends AudioStreamPlayer2D
# Variables you want to use across multiple functions should be declared above the rest of the script
var music_position = 0.0 # Since the music position is a float, I'm guessing it starts at 0.0
var music_isPlaying = false # This boolean flag can tell us if the music is currently playing
func _process(delta):
if Input.is_action_just_pressed("ui_accept"):
if !music_isPlaying: # If the music is not already playing
$AudioStreamPlayer2D.play(music_position) # Play from the currently saved position
music_isPlaying = true # Set the flag to tell us the music is playing
else: # Otherwise (if music is already playing)
music_position = $AudioStreamPlayer2D.get_playback_position() # Record the current position
$AudioStreamPlayer2D.stop() # Stop the music
music_isPlaying = false # Clear the flag to tell us music is no longer playing
I've included comments to describe what each line is for. Let me know if any of that needs clarification.
EDIT: Upon further reading, you could skip the manually-declared music_isPlaying
variable and use the built-in $AudioStreamPlayer2D.is_playing()
function instead.