0 votes

I'm making a music player in Godot and it's going pretty well, but I've run into a problem; I can only have the seek slider work when it's paused... here's my code:

func _on_SongTime_value_changed(value):
    if playing:
        pass
    else:
        audio_player.seek(value)

That's the current code I have and that basically makes it so that I'm not running into the problem that it's constantly calling the seek(value) function when the slider value progresses due to the song playing. Here's my old code:

func _on_SongTime_value_changed(value):
    audio_player.seek(value)

The problem with that is when the song progressed, causing the slider to move, the _on_SongTime_value_changed(value) event is called, causing it to call the seek(value) function over and over again due to this code:

func _process(delta):
    if playing:
        time_slider.value = audio_player.get_playback_position()

And this is kind of annoying to have to pause the song just to seek to a different timestamp, so I need to fix this ASAP ;).

Thanks in advance!

Godot version v3.4.2.stable.official [45eaa2daf]
in Engine by (193 points)

2 Answers

0 votes

One way to address this is to have a different function that is only called when you get user input...like, click, or keypress... And use your seek function then.

by (1,260 points)
0 votes

I ended up wrapping the seek() call in the value_changed handler with some checks on the current mouse state.

var ui_focused = false

func _on_SongTime_value_changed(value):
  if Input.is_mouse_button_pressed(BUTTON_LEFT) and ui_focused:
    audio_player.seek(value)

func _on_SongTime_mouse_entered():
  ui_focused = true

func _on_SongTime_mouse_exited():
  ui_focused = false

There's a small bug with this implementation. If you click and drag the mouse into the slider, you'll get the old playback issues. If you're willing to forego the ability to click and drag the slider you can also use Input.is_action_just_pressed("mouse_button") where the "mouse_button" action is mapped to left click in Godot's InputMap.

Looks like a known issue with this particular signal https://github.com/godotengine/godot/issues/37876

by (14 points)
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.