libGDX and javax.sound conflict? and scaling issue

331 views Asked by At

Been working on a few games, 2 2D, 1 3D game and 1 android app (mostly side projects) I have grown a liking to libgdx due to its flexibility to build games for android and computers through java, but there is a issue I have ran into. When the game is exported to either .jar or .apk it refuses to play audio....BUT if I just run it through intellij the audio works perfectly, so I would assume that libgdx sound engine is bugged on export, so I used the sound engine for my platformer and it spits out errors...for me to export from intellij into a working .jar file is to export the game through eclipse since intellij doesnt prebuild the metainf...
So the questions are:
1) Is there a secret way to export it with the default libGDX sound function?
MainClass without musicBox

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) Is there a way to make this musicbox code work with libjdx without spitting out the error?
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 the 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);
    }


The error it spits out:

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)And the last issue that is kinda bugging me but not as much as the sound, is when the game is pushed to android everything is zoomed out instead of being full screened as 1920x1080, its like everything is locked at 1280x720, is there a fix? (again not as important)

1

There are 1 answers

0
Naiakoa On BEST ANSWER

All problems solved, I just decided to give up and put together a whole new solution and issues were fixed. For those who wants a working audio system here is the new code for it (Solves issue 1 and 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();
}

}





To solve issue number 3 took me a couple hours, instead of just relying on default antics I just added in a sequence check and change the OrthographicCamera when needed on certain devices Solution is here:

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);
        }