libGDX和javax.sound有冲突吗?和扩展问题

时间:2014-11-29 04:18:01

标签: java android libgdx javax.sound.sampled

正在开发一些游戏,2个2D,1个3D游戏和1个Android应用程序(主要是侧面项目)我对libgdx非常喜欢,因为它可以灵活地通过java为Android和计算机构建游戏,但是有一个问题我遇到过。当游戏导出到.jar或.apk时它会拒绝播放音频....但是如果我只是通过intellij运行音频工作完美,那么我会假设libgdx声音引擎在导出时出错,所以我用过我的平台游戏的声音引擎,它吐出错误...让我从intellij导出到一个工作.jar文件是通过eclipse导出游戏,因为intellij没有预先构建metainf ...
所以问题是:
1)是否有秘密的方法导出它与默认的libGDX声音功能?
没有musicBox的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一起使用而不会出错?
MusicBox.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 with musicbox

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,是否有修复? (再一次不那么重要)

1 个答案:

答案 0 :(得分:0)

所有问题都解决了,我只是决定放弃并整理一个全新的解决方案,问题得到解决。对于那些想要一个有效的音频系统的人来说,这里有新代码(解决问题1和2):

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.graphics.Texture;

public class MusicBox {

private HashMap<String, Music> music;
private HashMap<String, Sound> sounds;

public MusicBox() {
    music = new HashMap<String, Music>();
    sounds = new HashMap<String, Sound>();
}

public void loadMusic(String path) {
    int slashIndex = path.lastIndexOf('/');
    String key;
    if(slashIndex == -1) {
        key = path.substring(0, path.lastIndexOf('.'));
    }
    else {
        key = path.substring(slashIndex + 1, path.lastIndexOf('.'));
    }
    loadMusic(path, key);
}
public void loadMusic(String path, String key) {
    Music m = Gdx.audio.newMusic(Gdx.files.local(path));
    music.put(key, m);
}
public Music getMusic(String key) {
    return music.get(key);
}
public void removeMusic(String key) {
    Music m = music.get(key);
    if(m != null) {
        music.remove(key);
        m.dispose();
    }
}
public void removeAll() {
    for(Object o : music.values()) {
        Music music = (Music) o;
        music.dispose();
    }
    music.clear();
    for(Object o : sounds.values()) {
        Sound sound = (Sound) o;
        sound.dispose();
    }
    sounds.clear();
}

}





解决3号问题花了我几个小时,而不是仅仅依靠我刚刚在序列检查中添加的默认滑动,并在某些设备上需要时更改OrthographicCamera解决方案在这里:

if(Gdx.app.getType() == Application.ApplicationType.Android) {
            camera = new OrthographicCamera(640, 360);
        }else if(Gdx.app.getType() == Application.ApplicationType.Desktop){
            camera = new OrthographicCamera(1280,720);
        }