Libgdx How to get SFX to fade out?
<p>I have a marching sound file which I play once a specific event occurs, but the issue comes down to having the file fade out in a specified amount of time before being stopped when the volume reaches 0. This project is for Desktop.</p>

<pre><code>Sound marching = Gdx.audio.newSound(Gdx.files.internal("sounds/test.wav"));
</code></pre>

<p>The first attempt I had at this was scheduling a task through the Timer:</p>

<pre><code>final long mId = marching.play(1f, 1f, 0);
for (float i = 1; i &gt; 0; i -= 0.01) {
    Timer.schedule(new Task() {
            @Override
            public void run() {
                marching.setVolume(mId, i);
            }
    }, 0.3f);
}
marching.stop();
</code></pre>

<p>This, however seemed to invoke marching#stop before marching#setVolume</p>

<p>I decided to take another apporach:</p>

<pre><code>float volume = 1;
final long mId = marching.play(volume, 1f, 0), startTime = TimeUtils                        .millis();
while (volume &gt; 0) {
    if (TimeUtils.millis() - startTime % 300 == 0) marching.setVolume(mId, volume -= 0.1);
}
marching.stop();
</code></pre>

<p>I thought that this would reduce the volume based on the amount of time allotted, yet it only made the program freeze.</p>

<p>Is there a straightforward way of fading SFX audio out or some manner of setting frequent sleeps so that I can lower the volume iteratively?</p>

<p>Thanks.</p>