Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (741)
Games in Android Showcase (225)
games submitted by our members
Games in WIP (823)
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 seem to get sound effects running  (Read 4070 times)
0 Members and 1 Guest are viewing this topic.
Offline Norakomi
« Posted 2014-05-06 09:21:11 »

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

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


and then tried to play it with this code:

1  
 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.<init>(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)


Offline T1663R

Junior Devvie


Medals: 2



« Reply #1 - Posted 2014-05-06 09:47:13 »

try it with "files.external" file and use a direct path to find out if the nullpointer is because ur internal path is wrong
Offline Norakomi
« Reply #2 - Posted 2014-05-06 10:04:59 »



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.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline T1663R

Junior Devvie


Medals: 2



« Reply #3 - Posted 2014-05-06 10:48:17 »

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
Offline Gibbo3771

JGO Kernel


Medals: 128
Projects: 5
Exp: 1 year


Currently inactive on forums :(


« Reply #4 - Posted 2014-05-06 10:55:18 »

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

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.

"This code works flawlessly first time and exactly how I wanted it"
Said no programmer ever
Offline T1663R

Junior Devvie


Medals: 2



« Reply #5 - Posted 2014-05-06 11:08:25 »

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.
?
but with the other stuff ur right ofc.
Offline Norakomi
« Reply #6 - Posted 2014-05-06 11:57:41 »

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

Offline Gibbo3771

JGO Kernel


Medals: 128
Projects: 5
Exp: 1 year


Currently inactive on forums :(


« Reply #7 - Posted 2014-05-06 12:31:58 »

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.
?
but with the other stuff ur right ofc.

Sorry I went a bit off the wall and never specified, Gdx.files.internal() looks in the assets folder is what I meant lol.

"This code works flawlessly first time and exactly how I wanted it"
Said no programmer ever
Offline Cero
« Reply #8 - Posted 2014-05-06 13:00:06 »

OP is using applets.
Might be relevant

Offline Norakomi
« Reply #9 - Posted 2014-05-06 13:08:20 »

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

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Cero
« Reply #10 - Posted 2014-05-06 13:52:54 »

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

Offline BurntPizza

« JGO Bitwise Duke »


Medals: 485
Exp: 7 years



« Reply #11 - Posted 2014-05-06 14:43:23 »

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).
Offline Norakomi
« Reply #12 - Posted 2014-05-06 17:43:30 »

its on the initialization line.

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

Offline BurntPizza

« JGO Bitwise Duke »


Medals: 485
Exp: 7 years



« Reply #13 - Posted 2014-05-06 18:40:03 »

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.
Offline Norakomi
« Reply #14 - Posted 2014-05-06 19:28:00 »


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

Offline BurntPizza

« JGO Bitwise Duke »


Medals: 485
Exp: 7 years



« Reply #15 - Posted 2014-05-06 19:37:18 »


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:

1  
2  
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."
Offline Norakomi
« Reply #16 - Posted 2014-05-06 20:01:54 »

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
1  
2  
3  
4  
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..
1  
2  
Gdx.files = new LwjglFiles();
Gdx.audio = new OpenALAudio(16, 9, 512);

That's all??

I'll try that later on tonight...

Offline BurntPizza

« JGO Bitwise Duke »


Medals: 485
Exp: 7 years



« Reply #17 - Posted 2014-05-06 20:08:16 »

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.

1  
2  
3  
4  
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.
Offline Norakomi
« Reply #18 - Posted 2014-05-06 21:23:46 »

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

Offline BurntPizza

« JGO Bitwise Duke »


Medals: 485
Exp: 7 years



« Reply #19 - Posted 2014-05-06 21:26:37 »

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.
Pages: [1]
  ignore  |  Print  
 
 

 
Ecumene (110 views)
2017-09-30 02:57:34

theagentd (146 views)
2017-09-26 18:23:31

cybrmynd (245 views)
2017-08-02 12:28:51

cybrmynd (241 views)
2017-08-02 12:19:43

cybrmynd (241 views)
2017-08-02 12:18:09

Sralse (254 views)
2017-07-25 17:13:48

Archive (872 views)
2017-04-27 17:45:51

buddyBro (1024 views)
2017-04-05 03:38:00

CopyableCougar4 (1577 views)
2017-03-24 15:39:42

theagentd (1373 views)
2017-03-24 15:32:08
List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05

SF/X Libraries
by SkyAphid
2017-03-02 06:38:56

SF/X Libraries
by SkyAphid
2017-03-02 06:38:32

SF/X Libraries
by SkyAphid
2017-03-02 06:38:05

SF/X Libraries
by SkyAphid
2017-03-02 06:37:51
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!