[LIBgdx] Can't seem to get sound effects running

Hi,
I cant seem to get a sound effect running in my game.
I originally didn’t start my game as a libgdx project, but only later in the development process did I add the gdx.jar library file, thinking this way I could use the music & sound class from libgdx.

So I am trying to get sound effect to play in my game, but it’s just not working.
I keep getting nullpointerexceptions.

I’m loading the soudfile in my constructor like this

Sound sound = Gdx.audio.newSound(Gdx.files
			.internal("data/powerup.ogg"));

and then tried to play it with this code:

 sound.play() 

but whatever I change i keep getting a nullpointerexception which i dont understand:
Does it maybe have to do with the fact that I havent started the project with libgdx and so i dont have all these other folders one usually have like [core], , [android] etc??

this is what my exception looks like:
java.lang.NullPointerException
at com.knightmare.KnightmareGame.(KnightmareGame.java:122)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at java.lang.Class.newInstance0(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)
at sun.applet.AppletPanel.createApplet(Unknown Source)
at sun.applet.AppletPanel.runLoader(Unknown Source)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

try it with “files.external” file and use a direct path to find out if the nullpointer is because ur internal path is wrong

Yeah, actually i tried using external also.
Plus i tried putting a wav file in my c:/ root and pointing it to there:
like …(“c:/powerup.ogg”));

But this also won’t work.

yeah i think thats wrong , external means ur user home path, look the paths up here and try again :
https://code.google.com/p/libgdx/wiki/FileHandling

LibGDX would throw a run time exception telling you the file can not be found, it is impossible to load a file that does not exist, therefore unless you null it or don’t initialize it, it will not be null.

LibGDX NEEDS to be setup in a specific way, you need to follow the setup guide on the wiki.

For the record, Gdx.files.xxx(path) looks in assets within the android project, which you probably don’t have.

yeah ive never tryed it that way and only pointed that stuff out to try help Norakomi, but anyway here :
https://code.google.com/p/libgdx/wiki/FileHandling
it says :
External - External files paths are relative to the SD card root on Android and to the home directory of the current user on desktop systems.
Absolute - Absolute files need to have their fully qualified paths specified.
so are you sure about [quote]For the record, Gdx.files.xxx(path) looks in assets within the android project, which you probably don’t have.
[/quote]
?
but with the other stuff ur right ofc.

That’s a whole bunch of stuff to look into already and should helpfully help me on my way to fixing this issue.

Ty guys

?
but with the other stuff ur right ofc.
[/quote]
Sorry I went a bit off the wall and never specified, Gdx.files.internal() looks in the assets folder is what I meant lol.

OP is using applets.
Might be relevant

Yes I use applet to test my game. But how would that make the difference between being or not being able to play sound??

well for one thing the sound effect must be in the jar I think in case of applets

On which line is the NPE occurring, the initialization line or the sound.play() line?

If it’s the initialization line (and the top of the stacktrace is in your code) then it’s because Gdx.audio or Gdx.files is null because libGDX isn’t set up (due to your project not being a libGDX app).

its on the initialization line.

So I guess I’ll just have to set up libgdx properly & cant just import the libraries…

Look in the constructor of LwjglApplication for how to initialize the Gdx stuff without actually instancing an app.

ehhh… this is one bridge too far for as my knowledge concerns… I have no idea how to do this.
And i have never used lwjglApplication before (have I?).

If you’ve used libGDX before you have.

Basically, Gdx.files and Gdx.audio are created in that constructor. You can do the same without having to construct a libGDX app:


Gdx.files = new LwjglFiles();
Gdx.audio = new OpenALAudio(16, 9, 512);

Put that somewhere in your app that will be run in the initialization phase.

You could also simply not rely on libGDX if all you’re using it for is file and audio handling, although I get why it’s a nice “cheat.”

I also wanna use libgdx later on for fonts, texturemaps, resizing & eventually deploying my games to different platforms and so forth, so I thought i’d better already get acquainted with libgdx.

So if i understand correctly if i would like my

Sound sound = Gdx.audio.newSound(Gdx.files
         .internal("data/powerup.ogg"));

sond.play();

code to work in my applet I should just add to the constructor…

Gdx.files = new LwjglFiles();
Gdx.audio = new OpenALAudio(16, 9, 512);

That’s all??

I’ll try that later on tonight…

Probably put the Gdx init in a static block rather than a constructor. It wouldn’t make sense to do it more than once, so put it in a static block where it will only be called once.


static {
    Gdx.files = new LwjglFiles();
    Gdx.audio = new OpenALAudio(16, 9, 512);
}

EDIT: I’m making no guarantees if this will go down well in an applet however. Then again applets don’t go down well period.

meehhhh…
.Lwjglfiles & .OpenAlAudio aren’t classes incorporated in my gdx.jar file.

F*ck it.

I’m just gonna have to start a gdx project i guess and move all my code over there…

They’re in the gdx-backends-lwjgl jar, as stated in the LWJGLApplication class.

Although going all-in or staying all-out is probably smart.