一直在开发一些游戏,2 2D,1 3D 游戏和 1 android 应用程序(主要是副项目)我已经喜欢 libgdx,因为它可以通过 java 为 android 和计算机构建游戏的灵活性,但是我有一个问题碰到了。当游戏导出到 .jar 或 .apk 时,它拒绝播放音频....但是如果我只是通过 intellij 运行它,音频工作得很好,所以我会假设 libgdx 声音引擎在导出时出现错误,所以我使用我的平台游戏的声音引擎,它会吐出错误......我从 intellij 导出到一个工作的 .jar 文件是通过 eclipse 导出游戏,因为 intellij 没有预先构建 metainf......
所以问题是:
1)是有没有用默认的 libGDX 声音功能导出它的秘密方法?
没有音乐盒的 MainClass
import com.badlogic.gdx.audio.Music;
Music music;
Music music = Gdx.audio.newMusic(Gdx.files.internal("Music/TheRoadOfMoor.wav"));
music.setVolume(0.2f);
music.setLooping(true);
music.play();
boolean isPlaying = music.isPlaying();
boolean isLooping = music.isLooping();
float position = music.getPosition();
2)有没有办法让这个音乐盒代码与 libjdx 一起工作而不会吐出错误?
音乐盒.java
import java.util.HashMap;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
public class MusicBox {
private static HashMap<String, Clip> clips;
private static int gap;
private static boolean mute = false;
public static void init() {
clips = new HashMap<String, Clip>();
gap = 0;
}
public static void load(String s, String n) {
if(clips.get(n) != null) return;
Clip clip;
try {
AudioInputStream ais = AudioSystem.getAudioInputStream(MusicBox.class.getResourceAsStream(s));
AudioFormat baseFormat = ais.getFormat();
AudioFormat decodeFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, baseFormat.getSampleRate(), 16, baseFormat.getChannels(), baseFormat.getChannels() * 2, baseFormat.getSampleRate(), false);
AudioInputStream dais = AudioSystem.getAudioInputStream(decodeFormat, ais);
clip = AudioSystem.getClip();
clip.open(dais);
clips.put(n, clip);
}
catch(Exception e) {
e.printStackTrace();
}
}
public static void play(String s) {
play(s, gap);
}
public static void play(String s, int i) {
if(mute) return;
Clip c = clips.get(s);
if(c == null) return;
if(c.isRunning()) c.stop();
c.setFramePosition(i);
while(!c.isRunning()) c.start();
}
public static void stop(String s) {
if(clips.get(s) == null) return;
if(clips.get(s).isRunning()) clips.get(s).stop();
}
public static void resume(String s) {
if(mute) return;
if(clips.get(s).isRunning()) return;
clips.get(s).start();
}
public static void loop(String s) {
loop(s, gap, gap, clips.get(s).getFrameLength() - 1);
}
public static void loop(String s, int frame) {
loop(s, frame, gap, clips.get(s).getFrameLength() - 1);
}
public static void loop(String s, int start, int end) {
loop(s, gap, start, end);
}
public static void loop(String s, int frame, int start, int end) {
stop(s);
if(mute) return;
clips.get(s).setLoopPoints(start, end);
clips.get(s).setFramePosition(frame);
clips.get(s).loop(Clip.LOOP_CONTINUOUSLY);
}
public static void setPosition(String s, int frame) {
clips.get(s).setFramePosition(frame);
}
public static int getFrames(String s) { return clips.get(s).getFrameLength(); }
public static int getPosition(String s) { return clips.get(s).getFramePosition(); }
public static void close(String s) {
stop(s);
clips.get(s).close();
}
}
MainClass.java:122 与音乐盒
import com.charedinferno.game.audio.MusicBox;
MusicBox music;
public void create(){
music.load("Musicx/level1.mp3", "level1");
music.play("level1");
music.loop("level1", 600, MusicBox.getFrames("level1") - 2200);
}
它吐出的错误:
Exception in thread "LWJGL Application" java.lang.NullPointerException
at com.game.audio.MusicBox.load(MusicBox.java:25)
at com.game.CharedInferno.show(MainClass.java:122)
at com.badlogic.gdx.Game.setScreen(Game.java:61)
at com.charedinferno.game.MainMenu$1.touchDown(MainMenu.java:101)
at com.badlogic.gdx.scenes.scene2d.InputListener.handle(InputListener.java:55)
at com.badlogic.gdx.scenes.scene2d.Actor.notify(Actor.java:182)
at com.badlogic.gdx.scenes.scene2d.Actor.fire(Actor.java:153)
at com.badlogic.gdx.scenes.scene2d.Stage.touchDown(Stage.java:278)
at com.badlogic.gdx.backends.lwjgl.LwjglInput.processEvents(LwjglInput.java:323)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:199)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)
3)最后一个让我烦恼但不如声音的问题是,当游戏被推送到 android 时,一切都被缩小而不是全屏显示为 1920x1080,就像所有东西都锁定在 1280x720,有没有使固定?(同样不那么重要)