[libGDX] Can't find an internal file even with the path.

Hey,

I’m trying, once again, to do something that requires me to use a file within the jar; and as usual I can’t get my code to find the file.

I’ve tried all of the (modifiers?) such as internal, absolute, classpath, etc… along with a few variations of the path such as “res/audio/tavern.ogg”, “/audio/tavern.ogg”, “tavern.ogg”, etc…
After all of those attempts failed I tried giving the exact path to the audio file would work but it didn’t.

Now I’m back here again to find out how to get my code working.

The method that’s messing up is below, the line that messed up is the first statement:

public static void newMusic(String path, boolean loop)
	{
		music = Gdx.audio.newMusic(Gdx.files.internal(path));
		music.setVolume(1.0f); //Sets volume to 100%
		music.setLooping(loop);
	}

The statement that uses the newMusic method is:

Audio.newMusic("/audio/tavern.ogg", true);

My folder structure is…

[tr][td]bin[/td][td]res[/td][td]src[/td][/tr]

…within the res folder it looks like…

[tr][td]audio[/td][td]levels[/td][td]textures[/td][/tr]

…and within the audio folder is “tavern.ogg”. So to get to tavern.ogg you would do “/res/audio/tavern.ogg”.

All files should be stored in the Android version with the folder name “assets” IIRC. This folder is shared between all the other projects (Desktop, iOS, HTML5). So stick your “res” folder in the “assets” folder (which is in the Android project) and see if that helps.

See https://code.google.com/p/libgdx/wiki/FileHandling#File_(Storage)_Types

I’m only using libGDX to play sound files. I’m not using it for anything else; the rest of the project is pure Java.

Have you tried this?
[icode]Audio.newMusic(“audio/tavern.ogg”, true);[/icode]

I personally like to put my “res” folder into another folder like “assets”. Then make the assets folder a source folder, and use the following:
[icode]Audio.newMusic(“res/audio/tavern.ogg”, true);[/icode]

Well, that seems awfully redundant. Either way, if you want to use libGDX internal storage, you need to put it in the Android projects “assets” folder as far as I can tell.

Have a look at the libGDX docs.

Internal Files: all the assets (images, audio files, etc.) that are packaged with your application are internal files. If you use the Setup UI, just drop them in your Android project’s assets folder.

Local Files: if you need to write small files to e.g. save a game state, use local files. These are in general private to your application. If you want a key/value store instead, you can also look into Preferences.

External Files: if you need to write big files, e.g. screenshots, or download files from the web, they should go on the external storage. Note that the external storage is volatile, a user can remove it or delete the files you wrote.

@davedes: Yep, I’ve tried both of those and neither work.

@jonjava: Well, I haven’t been able to find any other library to play all my audio files so I’m stuck with libGDX for now. Now, since nothing I try works and I’m going to assume that I can’t use libGDX like this without the specific file structure or whatever it’s called, I’ll have to go find something to play my audio files…

Edit: Found JLayer, gonna try it out. Still having problems finding the file even with JLayer…

Edit 2: Back to libGDX…

Edit 3: Welp screw it. Trying to find a way to play anything other than an midi file in Java is overly complicated and a pain in the but. I’ll just stick with .midi files since that’s the only thing I’ve ever been able to get working. =___=

This is wrong. You may end up in using non-libgdx standard project structure.

Don’t use .midi files if you can help it. They are a pain to work with and don’t always work across machines. I would probably use .ogg if I were you.

Welp, just solved my problem.

public static void playSound(String path) {
	    try {
	        Clip clip = AudioSystem.getClip();
	        AudioInputStream inputStream = AudioSystem.getAudioInputStream(new File(path));
	        clip.open(inputStream);
	        clip.start();
	    } catch (Exception e) {
	        System.err.println(e.getMessage());
	    

That’s not really solving the problem, since it won’t work once you package your game into a JAR. Since it isn’t supported by LibGDX, it won’t work on any other backend (Android, iOS, HTML5). Further, you don’t have any easy control over the clip, like looping or what have you. Lastly; there is no guarantee that OGG will work at all for other users, since AudioSystem might not find the correct SPIs on their machine.

Does your folder look like this?

Also, you should be using “Gdx.audio.newMusic” – not “Audio.newMusic”.

If you want a working example, you can try this (File -> Import -> Existing Projects into Workspace). If it runs and you hear audio, then it means you are doing something wrong on your own project (most likely not configuring the build path like we suggested).

Your application worked so I renamed my res folder to assets and added your code to my main method but it still gives a null pointer exception.

public static void main(String[] args)
	{
		Music m;
		m = Gdx.audio.newMusic(Gdx.files.internal("audio/main.mp3"));
		m.setLooping(false);
		m.play();
		Game game = new Game();
		game.frame.setResizable(true); //You can set this to resizable, the graphics just scale up.
		game.frame.setTitle("Sword_V3");
		game.frame.add(game);
		game.frame.pack();
		game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		game.frame.setLocationRelativeTo(null);
		game.frame.setVisible(true);
		
		game.start();
	}

I’ve tried all of the below paths:

  • /assets/audio/main.mp3
  • assets/audio/main.mp3
  • /audio/main.mp3
  • audio/main.mp3
  • main.mp3

You need to make “assets” a source folder for it to be included in the build path.

Also make sure that your actual folder structure looks like this. Notice that “assets” is not inside of “src”, as that will cause some issues. It’s a separate folder within your project folder.

When you go Project Properties -> Build Path, it should look like this:



Everything looks just like your images. When I add assets to the source I get the error below which I’m pretty sure means that I already have it set as a source folder.

Error while adding to build path
Reason:
Build path contains duplicate entry: ‘assets’ for project ‘Sword_V3’

After all of that I retried every possible path and none work.

Edit: Here’s my project folder http://www.mediafire.com/download/4925t2ba42t1aej/Sword_V3.zip

Have you tried refreshing the projects? You need to do that after you add resources in eclipse so that it can recognize them.

One thing that clearly does not match; your assets folder is listed in your Libraries tab, when it really should be removed from there and added to your Source Folder tab.

Anyways, there is actually a much bigger problem here. The NPE is because Gdx.audio is null (you can debug this yourself by looking at the stack trace and printing the null value). It’s null because you haven’t created a LwjglApplication, which is the entry point for any LibGDX application.

You are using LibGDX for audio, but you are using Java/Java2D for everything else. LibGDX is built on OpenGL and requires an OpenGL/OpenAL context for it to work. What you’re doing would be akin to cooking a pig in a toaster.

You should instead use TinySound or another sound API which does not depend on OpenGL/OpenAL context. Or, better yet, start building your game entirely with LibGDX, so that you don’t need to deal with crappy old Java2D. You can see the test project I posted earlier, which sets up a LibGDX context.

General rule of thumb when using LibGDX: don’t touch any AWT, Swing, or Java2D stuff.

I tried making a game in libGDX but it was way too confusing for me so I went back to Java 2D and started this project. I guess I’ll go try out TinySound then. Thanks for the help.

Strange, since I personally find Java2D pretty confusing … just so the GDX documentation folks might know, was there anything in particular you found confusing, or was it just the general experience?

Just because it’s too confusing the first time you try it doesn’t mean you should quit on it. LibGDX is fairly easy to learn, especially because of how high level it is. Learning LibGDX is well worth the time.

The majority of the tutorials I was following for using Tiles with libGDX were outdated and I couldn’t find any information on how to detect mouse wheel movement. There were a few other minor things but i can’t recall them atm.

I do admit that everything was a lot easier with libGDX but I’ll just stick with Java2D since it has, almost, everything I need at the moment.

I also managed to get TinySound working perfectly so my problem is solved.