v1.0.3: fixed audio issues on desktop caused by threading addition

This commit is contained in:
Evan Debenham
2021-08-30 18:44:48 -04:00
parent e3ffc875ba
commit 30c00f95b6

View File

@@ -131,36 +131,43 @@ public enum Music {
public void onCompletion(com.badlogic.gdx.audio.Music music) { public void onCompletion(com.badlogic.gdx.audio.Music music) {
//we do this in a separate thread to avoid graphics hitching while the music is prepared //we do this in a separate thread to avoid graphics hitching while the music is prepared
//FIXME this fixes graphics stutter but there's still some audio stutter, perhaps keep more than 1 player alive? //FIXME this fixes graphics stutter but there's still some audio stutter, perhaps keep more than 1 player alive?
new Thread(){ if (!DeviceCompat.isDesktop()) {
@Override new Thread() {
public void run() { @Override
synchronized (Music.INSTANCE) { public void run() {
if (trackList == null || trackList.length == 0 || player.isLooping() || music != player){ playNextTrack(music);
return;
}
Music.this.stop();
if (trackQueue.isEmpty()) {
for (int i = 0; i < trackList.length; i++) {
if (Random.Float() < trackChances[i]) {
trackQueue.add(trackList[i]);
}
}
if (shuffle) Collections.shuffle(trackQueue);
}
if (!enabled || trackQueue.isEmpty()) {
return;
}
play(trackQueue.remove(0), trackLooper);
} }
} }.start();
}.start(); } else {
//don't use a separate thread on desktop, causes errors and makes no performance difference(?)
playNextTrack(music);
}
} }
}; };
private synchronized void playNextTrack(com.badlogic.gdx.audio.Music music){
if (trackList == null || trackList.length == 0 || music != player || player.isLooping()){
return;
}
Music.this.stop();
if (trackQueue.isEmpty()) {
for (int i = 0; i < trackList.length; i++) {
if (Random.Float() < trackChances[i]) {
trackQueue.add(trackList[i]);
}
}
if (shuffle) Collections.shuffle(trackQueue);
}
if (!enabled || trackQueue.isEmpty()) {
return;
}
play(trackQueue.remove(0), trackLooper);
};
private synchronized void play(String track, com.badlogic.gdx.audio.Music.OnCompletionListener listener){ private synchronized void play(String track, com.badlogic.gdx.audio.Music.OnCompletionListener listener){
try { try {
player = Gdx.audio.newMusic(Gdx.files.internal(track)); player = Gdx.audio.newMusic(Gdx.files.internal(track));