问题
I am creating an Android App with Libgdx. The sound works pretty well, if I run the program in the desktop-version and on the emulator as well.
When I am testing the application on my android phone (Galaxy S3) there is no sound. Everything else works pretty fine on my device except the sound which is completely is missing. Do anyone know what the issue here could be?
Ask me for code or logs I’d like to post it for you!
Regards
The following example shows the way i play the sound:
import com.badlogic.gdx.audio.Sound;
public class LevelDesigner {
public LevelDesigner() {
Sound mp3Sound = Gdx.audio.newSound(Gdx.files.internal("data/sounds/sound.mp3"));
mp3Sound.loop();
}
}
回答1:
Sometimes the sound doesn't play the same frame it was loaded, try waiting one frame. Or playing it in another class.
Reference: Sound not playing after Loading
回答2:
Load your sound and verify if it is loaded before playing. I had the same problem and this works fine.
public void create(){
assetManager = new AssetManager();
assetManager.load("path/to/sound.mp3", Sound.class);
assetManager.finishLoading(); //Important!
}
//The load must be tested within the loop;
public void render(){
if (assetManager.isLoaded("path/to/sound.mp3")){
sound = assetManager.get("path/to/sound.mp3", Sound.class);
sound.play();
}
}
回答3:
The following code worked well on the device:
public class LevelDesigner {
private AssetManager assetManager;
public LevelDesinger {
assetManager = new AssetManager();
assetManager.load("data/sounds/loop.ogg", Music.class);
}
public boolean startMusic() {
if(assetManager.isLoaded("data/sounds/loop.ogg")) {
Music music = assetManager.get("data/sounds/loop.ogg", Music.class);
music.play();
music.setLooping(true);
}else {
System.out.println("not loaded yet");
return false;
}
return true;
}
}
Explananation: It is important to check if the sound has been loaded. If it isn't loaded an your try to play it doesn't through a exception so this is hard to find for a beginner ;)...
回答4:
Check that your mp3, ogg or wav isn't too short or too long! Sounds crazy but I found out about this issue here: http://github.com/libgdx/libgdx/issues/1366 and I tried a sound that I knew worked from another game and that worked.
My working sound, a piano keypress can be downloaded here:
http://www.martincapodici.com/wp-content/uploads/2014/08/a4.ogg
来源:https://stackoverflow.com/questions/20905364/no-sound-in-android-application-with-libgdx