+5 votes

the audio file is not set to loop in the import but it does any ways how can i stop it im using an animationplayer to play audio(SFX)

in Engine by (394 points)

4 Answers

–8 votes

What I do is assign false to the audio object's loop property. It sort of looks like this:

_ready():
  $audio_node.loop = false

I think that's how it works; I'm using something different to play audio.

by (2,232 points)

theres no loop property…

+17 votes

You might be using Ogg where you should be using WAV.

These two are meant for different types of sound in games:

WAV - high memory usage, very low CPU usage - good for short sound effects, especially if you want to play many sound effects on top of each other. Plays one-shot by default.

Ogg Vorbis - low memory usage, medium CPU usage - good for long sound effects like dialogue or music, lips by default.

I've realized my problems with looping audio were gone once I switched to using WAV sound effects instead of Ogg Vorbis.

by (199 points)

Agreed.

Ogg files are assumed to be looping background music, so they loop automatically, whilst Wav files are assumed to be soundFX, and by default do not loop.

These behaviors can changed by clicking on the Import tab, and selecting (or deselecting) the Loop checkbox, and then click the Reimport button.

this is the real answer

Following the directions in Brinux's comment (above) is what worked for me.

0 votes

Found the problem i just needed to set it to triggered in the animation player

by (394 points)
+2 votes

(TL;DR; Below)

I know this is an old post, but I needed help with a very similar issue. I had an AudioStreamPlayer2D node with an imported OGG Vorbis resource. I imported it with looping (the default) and during the game I did want it to loop repeatedly in the scene it was playing in (an animated shop dialog scene), but after the dialog ended, I didn't want the loop to end abruptly with the stop function call and when the audio is looping it won't trigger the finished() signal, so how could I get access to stop looping (after the animation and dialog finished), but let the sound play to completion first? After scrubbing the internet I found no answers, but trying a bunch of things, I finally found a simple two line solution:

var strm = $Music.stream as AudioStreamOGGVorbis
strm.set_loop(false)

Simply cast the stream resource as the class AudioStreamOGGVorbis and then you have access to the set_loop function. Then just set up the finished signal code to do what you need when the sound finishes (in my case loading the next scene after finishing the shop keepers dialog and animations - either canceling or opening the shop menu). Not sure if this will work forever, but it does work with version 3.2.

For completeness, you can also access the similar option for a WAV resource by casting the stream as AudioStreamSample and setting the loop_mode property to one of the LoopMode Constants called AudioStreamSample.LOOP_DISABLED.

var strm2 = $Sound.stream as AudioStreamSample
strm2.loop_mode = AudioStreamSample.LOOP_DISABLED

But not looping is the default. What about starting a WAV sound looping from GDScript? It is a little more complicated as you need to not only set the loop_mode with a constant, but you have to specify a starting point loop_begin (in bytes) and a loop ending point loop_end (also in bytes). This means you have to do a little math. You need to multiply the starting point and ending point in seconds by the mix_rate property to get the bytes. The simplest case is below, looping the entire clip.

var strm2 = $Sound.stream as AudioStreamSample
strm2.loop_mode = AudioStreamSample.LOOP_FORWARD
strm2.loop_begin = 0
strm2.loop_end = strm2.get_length() * strm2.mix_rate

And finally, you can start an OGG resource looping (if it were imported without looping, or looping was previously disabled):

var strm = $Music.stream as AudioStreamOGGVorbis
strm.set_loop(true)

You can even have that sweet battle scene intro slide right into a battle loop by setting a loop offset with the property loop_offset in seconds from the start. (Sorry, but no loop end for OGG Vorbis, but you can start a timer and then just use the stop() function.)

strm.loop_offset = 10

TL;DR;

You can access the loop boolean on an AudioStreamPlayer2D node playing an OGG Vorbis resource by simply casting the stream resource member on the node to AudioStreamOGGVorbis and calling the set_loop function. (At least in version 3.2) There is a similar cast for WAV, AudioStreamSample, but it is slightly more complicated. See Above for details on both.

Reference: https://docs.godotengine.org/en/3.2/classes/class_audiostream.html#class-audiostream

by (18 points)
edited by

If you just need to access the properties directly when importing without accessing them during playback/scripting, then you just need to click on the AudioStreamPlayer2D node, then click on the drop down arrow beside the resource in the properties panel, choose edit (but you can't edit them in the fields shown, you now have to click on the import tab next to the scene panel, and there you can edit the values, but one last step is you have to reimport the resource to save the settings. Convoluted, but that is the easiest way I found to get there. If anyone knows any easier method, please share. Thanks!

Sorry. Apparently you can click on the file itself in the resources panel below the scenes panel and then click on the import panel. I'm still getting used to the interface and I thought that clicking on the node and then import panel would bring up the resource attached. That said, the code in the answer above is the only way I know to access it from the script if adding the resource through the AudioStreamPlayer2D node. There is example code for loading a resource completely from script and then accessing it, but I find this method much more succinct and easy to implement, plus you can do all your other modifications to the resource straight from the node keeping your code cleaner in my opinion.

Loading Resource From GDScript Question and Answer

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.