Skip to content

Commit 1550c92

Browse files
committed
Change music loading thread to Gdx timer task + control openAL bug in desktop.
1 parent 7a7701a commit 1550c92

File tree

2 files changed

+74
-38
lines changed

2 files changed

+74
-38
lines changed

blade-engine/src/com/bladecoder/engine/model/MusicManager.java

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package com.bladecoder.engine.model;
22

3+
import com.badlogic.gdx.Gdx;
4+
import com.badlogic.gdx.Application.ApplicationType;
35
import com.badlogic.gdx.audio.Music;
46
import com.badlogic.gdx.utils.Json;
57
import com.badlogic.gdx.utils.Json.Serializable;
68
import com.badlogic.gdx.utils.JsonValue;
9+
import com.badlogic.gdx.utils.Timer;
10+
import com.badlogic.gdx.utils.Timer.Task;
711
import com.bladecoder.engine.actions.ActionCallback;
812
import com.bladecoder.engine.anim.MusicVolumeTween;
913
import com.bladecoder.engine.assets.AssetConsumer;
@@ -30,6 +34,18 @@ public class MusicManager implements Serializable, AssetConsumer {
3034
transient private boolean isPaused = false;
3135
private MusicVolumeTween volumeTween;
3236

37+
private final Task backgroundLoadingTask = new Task() {
38+
@Override
39+
public void run() {
40+
if (EngineAssetManager.getInstance().isLoading()) {
41+
cancel();
42+
Timer.post(backgroundLoadingTask);
43+
} else {
44+
retrieveAssets();
45+
}
46+
}
47+
};
48+
3349
public void playMusic() {
3450
if (music != null && !music.isPlaying()) {
3551

@@ -38,6 +54,15 @@ public void playMusic() {
3854
music.setLooping(desc.isLoop());
3955
music.setVolume(desc.getVolume());
4056
} catch (Exception e) {
57+
58+
// DEAL WITH OPENAL BUG
59+
if (Gdx.app.getType() == ApplicationType.Desktop && e.getMessage().contains("40963")) {
60+
EngineLogger.debug("!!!!!!!!!!!!!!!!!!!!!!!ERROR playing music trying again...!!!!!!!!!!!!!!!");
61+
setMusic(desc);
62+
63+
return;
64+
}
65+
4166
EngineLogger.error("Error Playing music: " + desc.getFilename(), e);
4267
}
4368
}
@@ -65,29 +90,33 @@ public void stopMusic() {
6590
public void setMusic(MusicDesc d) {
6691
EngineLogger.debug(">>>SETTING MUSIC.");
6792
stopMusic();
68-
volumeTween = null;
93+
volumeTween = null;
6994
currentMusicDelay = 0;
7095

7196
if (d != null) {
72-
if (desc != null)
97+
if (desc != null) {
7398
dispose();
99+
}
74100

75101
desc = new MusicDesc(d);
76102

77-
// Load and play the voice file in a different Thread to avoid
103+
// Load the music file in background to avoid
78104
// blocking the UI
79-
new Thread() {
80-
@Override
81-
public void run() {
82-
retrieveAssets();
83-
}
84-
}.start();
105+
loadTask();
85106
} else {
86107
dispose();
87108
desc = null;
88109
}
89110
}
90111

112+
private void loadTask() {
113+
loadAssets();
114+
115+
backgroundLoadingTask.cancel();
116+
// Timer.schedule(backgroundLoadingTask, 0.2f);
117+
Timer.post(backgroundLoadingTask);
118+
}
119+
91120
public void setVolume(float volume) {
92121
if (desc != null)
93122
desc.setVolume(volume);
@@ -110,7 +139,7 @@ public void leaveScene(MusicDesc newMusicDesc) {
110139
return;
111140

112141
if (desc != null) {
113-
currentMusicDelay = 0;
142+
currentMusicDelay = 0f;
114143
stopMusic();
115144
dispose();
116145
}
@@ -177,10 +206,12 @@ public void loadAssets() {
177206
@Override
178207
public void retrieveAssets() {
179208
if (music == null && desc != null) {
180-
209+
// Check if not loaded, this happens when setting a cached scene
181210
if (!EngineAssetManager.getInstance().isLoaded(EngineAssetManager.MUSIC_DIR + desc.getFilename())) {
182-
loadAssets();
183-
EngineAssetManager.getInstance().finishLoading();
211+
// Load the music file in background to avoid
212+
// blocking the UI
213+
loadTask();
214+
return;
184215
}
185216

186217
EngineLogger.debug("RETRIEVING MUSIC: " + desc.getFilename());

blade-engine/src/com/bladecoder/engine/model/VoiceManager.java

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import com.badlogic.gdx.utils.Json;
77
import com.badlogic.gdx.utils.Json.Serializable;
88
import com.badlogic.gdx.utils.JsonValue;
9+
import com.badlogic.gdx.utils.Timer;
10+
import com.badlogic.gdx.utils.Timer.Task;
911
import com.bladecoder.engine.assets.AssetConsumer;
1012
import com.bladecoder.engine.assets.EngineAssetManager;
1113
import com.bladecoder.engine.util.EngineLogger;
@@ -32,6 +34,29 @@ public class VoiceManager implements Serializable, AssetConsumer {
3234
transient private boolean isPaused = false;
3335
transient private TextManager textManager = null;
3436

37+
private final Task backgroundLoadingTask = new Task() {
38+
@Override
39+
public void run() {
40+
try {
41+
if (EngineAssetManager.getInstance().isLoading()) {
42+
cancel();
43+
Timer.post(backgroundLoadingTask);
44+
} else {
45+
retrieveAssets();
46+
47+
if (voice != null)
48+
voice.play();
49+
}
50+
51+
} catch (GdxRuntimeException e) {
52+
EngineLogger.error(e.getMessage());
53+
voice = null;
54+
fileName = null;
55+
textManager.next();
56+
}
57+
}
58+
};
59+
3560
public VoiceManager(TextManager textManager) {
3661
this.textManager = textManager;
3762
}
@@ -63,18 +88,12 @@ public void play(String fileName) {
6388

6489
this.fileName = fileName;
6590

66-
if (fileName != null) {
67-
// Load and play the voice file in a different Thread to avoid
91+
if (fileName != null) {
92+
// Load and play the voice file in background to avoid
6893
// blocking the UI
69-
new Thread() {
70-
@Override
71-
public void run() {
72-
retrieveAssets();
73-
74-
if (voice != null)
75-
voice.play();
76-
}
77-
}.start();
94+
loadAssets();
95+
backgroundLoadingTask.cancel();
96+
Timer.post(backgroundLoadingTask);
7897
}
7998
}
8099

@@ -114,20 +133,6 @@ public void loadAssets() {
114133
public void retrieveAssets() {
115134
if (voice == null && fileName != null) {
116135

117-
if (!EngineAssetManager.getInstance().isLoaded(EngineAssetManager.VOICE_DIR + fileName)) {
118-
loadAssets();
119-
120-
try {
121-
EngineAssetManager.getInstance().finishLoading();
122-
} catch (GdxRuntimeException e) {
123-
EngineLogger.error(e.getMessage());
124-
voice = null;
125-
fileName = null;
126-
textManager.next();
127-
return;
128-
}
129-
}
130-
131136
EngineLogger.debug("RETRIEVING VOICE: " + fileName);
132137

133138
voice = EngineAssetManager.getInstance().get(EngineAssetManager.VOICE_DIR + fileName, Music.class);

0 commit comments

Comments
 (0)