Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (522)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (591)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  [libGDX] Can't find an internal file even with the path.  (Read 2942 times)
0 Members and 1 Guest are viewing this topic.
Offline tyeeeee1
« Posted 2013-07-13 19:01:11 »

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:
1  
2  
3  
4  
5  
6  
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:
1  
Audio.newMusic("/audio/tavern.ogg", true);


My folder structure is...

binressrc

...within the res folder it looks like...
audiolevelstextures

...and within the audio folder is "tavern.ogg". So to get to tavern.ogg you would do "/res/audio/tavern.ogg".
Offline jonjava
« Reply #1 - Posted 2013-07-13 21:08:49 »

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

Offline tyeeeee1
« Reply #2 - Posted 2013-07-13 21:36:00 »

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.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline davedes
« Reply #3 - Posted 2013-07-13 21:44:20 »

Have you tried this?
Audio.newMusic("audio/tavern.ogg", true);



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:
Audio.newMusic("res/audio/tavern.ogg", true);

Offline jonjava
« Reply #4 - Posted 2013-07-13 21:47:32 »

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.

Offline tyeeeee1
« Reply #5 - Posted 2013-07-13 21:57:45 »

@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. =___=
Offline ReBirth
« Reply #6 - Posted 2013-07-14 02:32:46 »

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.
This is wrong. You may end up in using non-libgdx standard project structure.

Offline Longarmx
« Reply #7 - Posted 2013-07-14 06:50:40 »

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.

Offline tyeeeee1
« Reply #8 - Posted 2013-07-14 13:51:24 »

Welp, just solved my problem.

1  
2  
3  
4  
5  
6  
7  
8  
9  
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());
       
Offline davedes
« Reply #9 - Posted 2013-07-14 14:46:47 »

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).

https://www.dropbox.com/s/wcwqr7cq7hoyfvz/testproj.zip

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline tyeeeee1
« Reply #10 - Posted 2013-07-14 14:57:29 »

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.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
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

Offline davedes
« Reply #11 - Posted 2013-07-14 15:05:38 »

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:

http://i.imgur.com/ggfP9gr.png
http://i.imgur.com/V1SKCGL.png
http://i.imgur.com/ZZDDxq7.png


Offline tyeeeee1
« Reply #12 - Posted 2013-07-14 15:25:39 »

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
Offline jonjava
« Reply #13 - Posted 2013-07-14 18:19:58 »

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

Offline davedes
« Reply #14 - Posted 2013-07-14 18:41:36 »

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.

Offline tyeeeee1
« Reply #15 - Posted 2013-07-14 18:55:22 »

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.
Offline sproingie

JGO Kernel


Medals: 202



« Reply #16 - Posted 2013-07-15 21:44:26 »

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?
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 138
Projects: 4
Exp: 3 years



« Reply #17 - Posted 2013-07-15 21:46:55 »

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.
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.
Offline tyeeeee1
« Reply #18 - Posted 2013-07-15 23:10:23 »

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?


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.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

trollwarrior1 (34 views)
2014-11-22 12:13:56

xFryIx (73 views)
2014-11-13 12:34:49

digdugdiggy (52 views)
2014-11-12 21:11:50

digdugdiggy (46 views)
2014-11-12 21:10:15

digdugdiggy (40 views)
2014-11-12 21:09:33

kovacsa (66 views)
2014-11-07 19:57:14

TehJavaDev (70 views)
2014-11-03 22:04:50

BurntPizza (68 views)
2014-11-03 18:54:52

moogie (83 views)
2014-11-03 06:22:04

CopyableCougar4 (82 views)
2014-11-01 23:36:41
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!