Audio in _procces

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

I’m trying to add underwater sound effect and I need to detect this every frame.
But my sound effect only loops 0.1ms. How can ı fix that problem?

 func _process(delta):
    	if transform.origin.y < -7.2:
    		$"/root/World/UI/Effect/UnderWater".show()
    		$UnderWater.play()
    		vel.y += 0.1
    		freeCamera = true
    	else:
    		$"/root/World/UI/Effect/UnderWater".hide()
    		$UnderWater.stop()
    		freeCamera = false
:bust_in_silhouette: Reply From: Dlean Jeans

Looks like you’re restarting the UnderWater sound every frame underwater.
Try:

if not $UnderWater.playing:
    $UnderWater.play()

Bonus: if you don’t wanna start the sound at the beginning every time, record the playback position before you stop it:

var playback_pos = 0

if underwater:
    if not $UnderWater.playing:
        $UnderWater.play(playback_pos)
else:
    playback_pos = $UnderWater.get_playback_position()
    $UnderWater.stop()

Thank you very much. It worked!

Erdo | 2019-06-03 11:51