问题
When using LibGDX's AssetsManager, I've implement an AssetsManager for managing Sounds, and made it global. The files are all located within
../data/sounds/.*
(example: ../data/sounds/buttons/buttonl.mp3)
Then I load sound files into the manager, and then include some code to play those sounds. Code below:
public final class Sounds {
private static HashMap<String, HashMap<String, String>> entries = new HashMap<String, HashMap<String, String>>();
private static HashMap<String, String> currentPackage = new HashMap<String, String>();
public static Sound sound;
public Sounds() {
addPath(
"Main",
"bgm", "data/sounds/main/title.mp3",
"buttonl", "data/sounds/buttons/buttonl.mp3",
"buttonh", "data/sounds/buttons/buttonh.mp3");
//... More paths with more sound assets
}
public static boolean addPath(String packageName, String... args) {
if(!entries.containsKey(packageName)) {
entries.put(packageName, new HashMap<String, String>());
for(int i = 0; i < args.length; i ++)
entries.get(packageName).put(args[i], args[++i]);
return true;
}
return false;
}
public static void loadSoundAssets(String packageName) {
for(String str : entries.get(packageName).values())
s_manager.load(str, Sound.class);
s_manager.finishLoading();
}
public static boolean play(String file) {
if(currentPackage.containsKey(file)) {
sound = s_manager.get(file, Sound.class);
sound.play();
return true;
}
return false;
}
}
After creating the Sound class in a higher class with
public class Game implements ApplicationListener{
public Sounds gameSounds = new Sounds();
public Screen currentScreen = new MainMenuScreen();
//... More class variables and methods
public void render() {
currentScreen.update();
}
}
The problem comes when I call
public class MainScreen() extends Screen {
public MainScreen() {
Sounds.loadSoundAssets("Main");
//... More constructor variables and methods
}
//... Class variables and methods
public void update() {
if(s_manager.update()) {
if(play.getBoundingRectangle().contains(x, y))
Sounds.play("buttonl");
}
}
}
s_manager.update() returns true, but then it tries to play the sound file "buttonl" and throws this error:
Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: Asset not loaded: buttonh
at com.badlogic.gdx.assets.AssetManager.get(AssetManager.java:121)
at device.sounds.Sounds.play(Sounds.java:146)
at game.screens.MainMenuScreen.update(MainMenuScreen.java:63)
at menu.StateManager.render(StateManager.java:89)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:190)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:110)
How should I be using the AssetsManager to fix this loading/playing problem? I want to use the AssetsManager, but currently, my solution is to use
sound = Gdx.audio.newSound(Gdx.files.internal(currentPackage.get(file)));
回答1:
I believe that in your play method, you are calling AssetManager.get on the key rather than its value in currentPackage. I think a correct play method would be:
public static boolean play(String file) {
if(currentPackage.containsKey(file)) {
sound = s_manager.get(currentPackage.get(file), Sound.class);
sound.play();
return true;
}
return false;
}
来源:https://stackoverflow.com/questions/20830760/libgdx-assetsmanager-sounds-loaded-but-not-loaded-error