For audio stream players, I think, though have not tested, that you can get the length of whatever sample is playing using something like this:
var audio_player = get_node("AudioStreamPlayer")
# quick error checking:
if (audio_player != null):
if (audio_player.stream != null):
var audio_length = audio_player.stream.get_length()
print ("Audio is ", audio_length, " seconds long")
For video players, it does not look like there is a way to get the length automatically. The VideoStream class does not provide a get_length
function. That said, there is a stream_position
function, which you can set and get. I'm not sure if it would work, since it assumes Godot clamps the stream_position
variable to the length of the video player, but maybe you could use code like this to get the end position of a VideoPlayer node:
var video_player = get_node("VideoPlayer")
if (video_player != null):
if (video_player .stream != null):
var stored_length = video_player.stream_position
video_player.stream_position = 99999999
var video_length = video_player.stream_position
print ("Video is ", video_length, " seconds long")
# set the video back to whatever position it was prior to getting the length
video_player.stream_position = stored_length