0 votes

I have an AudioStreamPlayer which I sometimes wish to play for only x seconds.

I know I can set up a timer, and stop the player once the timer is finished, but is there any built in way to do this without having to go through that rigamorole? Something along the lines of

$Player.play(duration_seconds)

? I may want to do this often and don't want to create too many timers.

Also, I realize I could try to cut the .wav file in a separate software and use the cut version, but this doesn't always work. For example, the Metal medieval construction sound file from this site, when cut in VLC media player, leaves out the initial strike sound and so trying to cut the file from the beginning to x seconds doesn't work as a solution with that software (I'm not sure if better options exist, but google seems to suggest VLC in top searches).

Godot version v3.2.1.stable.official
in Engine by (63 points)

Maybe if you use _process(delta) you can use the delta variant to stop it?
Edit:
Looking up the AudioStreamPlayer.get_playback_position() method could help

yield(get_tree().create_timer(5.0), "timeout") will generate a 5 second wait time before the function which calls it continues (automatically).

Sound buffering can mess with any timer implementation.
https://docs.godotengine.org/en/stable/tutorials/audio/sync_with_audio.html

First you have godot buffer, then OS buffering may be involved (Hello Linux/Pulse audio).

The Sound server may be on a separate thread. Your GDScript timer may be halted while it keeps playing file to some buffer.(not sure about what try asking engine devs)

If you need to play 5 seconds of larger sound, cut 5 seconds in an audio editor and save in a separate file.

2 Answers

+1 vote
Best answer

Option 1:Use Player.seek (float to_position) before Player.play(). For full AudioStreamPlayer API see https://docs.godotengine.org/en/stable/classes/class_audiostreamplayer.html

Player.play ( float from_position=0.0 ) do exist but fails on my android device so i advice first seek then play.

Option 2: Cut out needed part in https://www.audacityteam.org

by (886 points)
selected by

Doesn’t the seek function only allow you to specify the start time, and not the stop?

So option 1 here unfortunately doesn't work because it only lets me choose a starting time, and not a stopping time, for my clip (unless I misunderstood something).

I did download audacity (option 2) and used it to create my clips (thanks for the recommendation!). It is a much better software for this than VLC, but I do think it would be useful for the devs to add an option to "play_until" on audio stream players (if they have the time).

0 votes

I use animations or timers. I'm already using them to handle timing for the audio player. I use the same animation that fades the screen out to fade out bgm audio, the same anim that spins my bullets plays the impact sfx on the last frame, etc. If there's no relevant animation, I use a timer.

It seems like you might be using the same audio file, just cutting it short for one use case, which would be a perfect time to just make a shortened copy of the audio file. I use VLC, and can say it's more a media player than a media editor. I didn't even know it had such functionality. Audacity would likely not cause the issue you got from VLC.

If it is in fact multiple different audio files all needing to be cut to a specific duration depending on circumstances that vary in-game, then I believe you will want to either use Timers or Animations.

I'm no expert, there may yet be a better solution or at least a more efficient one.

by (26 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.