You can use a Tween to change the AudioStreamPlayer dB, so that the player is silent after a set amount of time:
var tw = Tween.new()
var asp = AudioStreamPlayer.new()
func _ready():
# add an AudioStreamPlayer and Tween to scene
add_child(asp)
add_child(tw)
tw.connect("tween_all_completed",self,'_on_tween_all_completed')
func play_for(start_t = 0, dur = 1.5):
# start_t delays the playing start; dur is how long to play after start
var start_dB = 0
var final_dB = -60 # adjust this as needed to silence or just lower volume after duration
tw.remove_all() # remove all previous tween settings; reset_all() didn't work
asp.set_volume_db(start_dB)
var transition_duration = 0.1
var transition_type = Tween.TRANS_QUART
# avoid popping by fading in at start_t
tw.interpolate_property(asp,'volume_dB',-60,start_dB,0.05,transition_type,Tween.EASE_IN,start_t)
# fade out so sound ends after dur seconds
tw.interpolate_property(asp, "volume_db", start_dB, final_dB, transition_duration, transition_type, Tween.EASE_IN, start_t + dur - transition_duration)
# delay the start of playing; playing from the start of the audio clip at time start_t
tw.interpolate_deferred_callback(asp,start_t,'play',0)
tw.start()
func _on_tween_all_completed():
#could stop the audio player with asp.stop(), but prefer to emit a signal to handle action elsewhere
emit_signal("finished")