How to play a single part of an audio stream?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By ClumsyDog

Basically, I want to use my audio stream like how you would use a sprite sheet, except it’s filled with assorted sounds rather than sprites. How do I make it play/trim it to only play a single part (in editor I mean, not actually trimming the file so I can reuse it in the future), rather than the whole thing? Thanks in advance!

:bust_in_silhouette: Reply From: bloodsign

You can pass an argument to play ( float from_position=0.0 )

Which allows you to play form the position.
Then you could use get_playback_position ( ) and then flag the audio player to stop when it reaches that desired duration.

Pseudo code:

export var start = 10.0
export var end = 20.0
onready var asp = $AudioStreamPlayer

func some_func():
  if !asp.playing:
    asp.play(start)
  if asp.get_playback_position() >= end:
    asp.stop()

PS. I didn’t test this code snippet so take it with a grain of coffee and salt
You could also use seek() to set the position of where the audio would start playing.

Check the docs here for more info about Audio:

Thanks! I didn’t know those functions existed!

ClumsyDog | 2021-03-18 07:10