Topic was automatically imported from the old Question2Answer platform.
Asked By
SF123
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:
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 ;).
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.
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.