v2.2.0: added support for a fade out transition when changing music
This commit is contained in:
@@ -277,6 +277,7 @@ public class Game implements ApplicationListener {
|
|||||||
|
|
||||||
inputHandler.processAllEvents();
|
inputHandler.processAllEvents();
|
||||||
|
|
||||||
|
Music.INSTANCE.update();
|
||||||
Sample.INSTANCE.update();
|
Sample.INSTANCE.update();
|
||||||
scene.update();
|
scene.update();
|
||||||
Camera.updateAll();
|
Camera.updateAll();
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ package com.watabou.noosa.audio;
|
|||||||
|
|
||||||
import com.badlogic.gdx.Gdx;
|
import com.badlogic.gdx.Gdx;
|
||||||
import com.watabou.noosa.Game;
|
import com.watabou.noosa.Game;
|
||||||
|
import com.watabou.utils.Callback;
|
||||||
import com.watabou.utils.DeviceCompat;
|
import com.watabou.utils.DeviceCompat;
|
||||||
import com.watabou.utils.Random;
|
import com.watabou.utils.Random;
|
||||||
|
|
||||||
@@ -41,6 +42,10 @@ public enum Music {
|
|||||||
private boolean enabled = true;
|
private boolean enabled = true;
|
||||||
private float volume = 1f;
|
private float volume = 1f;
|
||||||
|
|
||||||
|
private float fadeTime = -1f;
|
||||||
|
private float fadeTotal = -1f;
|
||||||
|
private Callback onFadeOut = null;
|
||||||
|
|
||||||
String[] trackList;
|
String[] trackList;
|
||||||
float[] trackChances;
|
float[] trackChances;
|
||||||
private final ArrayList<String> trackQueue = new ArrayList<>();
|
private final ArrayList<String> trackQueue = new ArrayList<>();
|
||||||
@@ -122,6 +127,29 @@ public enum Music {
|
|||||||
play(trackQueue.remove(0), trackLooper);
|
play(trackQueue.remove(0), trackLooper);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public synchronized void fadeOut(float duration, Callback onComplete){
|
||||||
|
fadeTotal = duration;
|
||||||
|
fadeTime = 0f;
|
||||||
|
onFadeOut = onComplete;
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized void update(){
|
||||||
|
if (fadeTotal > 0f){
|
||||||
|
fadeTime += Game.elapsed;
|
||||||
|
|
||||||
|
if (player != null) {
|
||||||
|
player.setVolume(volumeWithFade());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fadeTime >= fadeTotal) {
|
||||||
|
fadeTime = fadeTotal = -1f;
|
||||||
|
if (onFadeOut != null){
|
||||||
|
onFadeOut.call();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private com.badlogic.gdx.audio.Music.OnCompletionListener trackLooper = new com.badlogic.gdx.audio.Music.OnCompletionListener() {
|
private com.badlogic.gdx.audio.Music.OnCompletionListener trackLooper = new com.badlogic.gdx.audio.Music.OnCompletionListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onCompletion(com.badlogic.gdx.audio.Music music) {
|
public void onCompletion(com.badlogic.gdx.audio.Music music) {
|
||||||
@@ -167,7 +195,7 @@ public enum Music {
|
|||||||
try {
|
try {
|
||||||
player = Gdx.audio.newMusic(Gdx.files.internal(track));
|
player = Gdx.audio.newMusic(Gdx.files.internal(track));
|
||||||
player.setLooping(looping);
|
player.setLooping(looping);
|
||||||
player.setVolume(volume);
|
player.setVolume(volumeWithFade());
|
||||||
player.play();
|
player.play();
|
||||||
if (listener != null) {
|
if (listener != null) {
|
||||||
player.setOnCompletionListener(listener);
|
player.setOnCompletionListener(listener);
|
||||||
@@ -207,7 +235,15 @@ public enum Music {
|
|||||||
public synchronized void volume( float value ) {
|
public synchronized void volume( float value ) {
|
||||||
volume = value;
|
volume = value;
|
||||||
if (player != null) {
|
if (player != null) {
|
||||||
player.setVolume( value );
|
player.setVolume( volumeWithFade() );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private synchronized float volumeWithFade(){
|
||||||
|
if (fadeTotal > 0f){
|
||||||
|
return Math.max(0, volume * ((fadeTotal - fadeTime) / fadeTotal));
|
||||||
|
} else {
|
||||||
|
return volume;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user