Author Topic: SoundSystem playbackgroundmusic null pointer  (Read 3078 times)

Offline metapod

  • bit
  • Posts: 5
    • View Profile
SoundSystem playbackgroundmusic null pointer
« on: January 31, 2014, 11:05:13 AM »
I'm having a hard time getting started with soundsystem playing an ogg background music file...

Any idea why calling playBGM gives me a null pointer? The file DOES exist. Thanks!

Code: [Select] [nofollow]
public class SoundPlayer {
 
  private SoundSystem soundSystem;
  public static final String BGM = "BGM";
 
  public SoundPlayer() {
    try {
      SoundSystemConfig.addLibrary(LibraryJavaSound.class);
      SoundSystemConfig.setCodec("ogg", CodecJOrbis.class);
      SoundSystemConfig.setCodec("wav", CodecWav.class);
    }
    catch(SoundSystemException e)
    {
      System.err.println("error linking with the plug-ins" );
    }
   
    SoundSystem soundSystem = new SoundSystem();
   
    soundSystem.setMasterVolume(1.0f);
    soundSystem.setVolume(BGM, 1.0f);
  }
 
  public void playBGM(String name) {
    File f = new File("res/music/" + name);
    if (!f.exists()) return;

    try {
      URL path = f.toURI().toURL();
      System.out.println(path);
     
      soundSystem.backgroundMusic(BGM, path, name, true);
      soundSystem.setVolume(BGM, 1.0f);
    } catch (MalformedURLException e) {

      e.printStackTrace();
    }
  }
}

Offline Paul

  • Administrator
  • double
  • *****
  • Posts: 3473
  • Developer
    • View Profile
    • PaulsCode.Com
Re: SoundSystem playbackgroundmusic null pointer
« Reply #1 on: January 31, 2014, 11:35:59 AM »
I don't think the problem is anything to do with the file or with the backgroundMusic function itself, but rather the "null pointer" in question is referring to the soundSystem variable itself.  In your playBGM function, you are referencing the global varriable 'soundSystem', but that variable isn't set in the constructor (a local variable is instantiated instead).

You should be able to fix the problem by changing the line in the constructor:
Code: [Select]
    SoundSystem soundSystem = new SoundSystem();to:
Code: [Select]
    soundSystem = new SoundSystem();
Device: Samsung Galaxy Nexus i515
CPU: TI OMAP4460, 1.2 GHz (dual core, ARM Cortex-A9)
GPU: PowerVR SGX540, 307 MHz
RAM: 1 GB
Resolution: 720 x 1280
Rom: omni-4.4.4-20141014-toro-FML KitKat 4.4.4, rooted

Device: Eee PC 1015PEM
CPU: Intel Atom N550, 1.5 GHz (dual core, x86)
GPU: Intel GMA 3150, 200 MHz (dual core)
RAM: 2GB
Resolution: 1024 x 600
Rom: android-x86-4.3-20130725 Jelly Bean 4.3, rooted

Offline metapod

  • bit
  • Posts: 5
    • View Profile
Re: SoundSystem playbackgroundmusic null pointer
« Reply #2 on: January 31, 2014, 02:00:18 PM »
Oh wow, I'm an idiot... guess that's what happens when you're copy pasting examples not paying enough attention. I'm also not really used to Java and newing up everything (I normally do embedded C++).

Well I have a new problem when checking if LibraryLWJGLOpenAL is compatible I get:
 
Checking if LWJGL OpenAL is compatible...
Error in class 'SoundSystemConfig'
    InvocationTargetException thrown while attempting to invoke method 'Library.getTitle' in method 'getLibraryTitle'
Error in class 'SoundSystemConfig'
    Method 'Library.libraryCompatible' returned 'null' in method 'libraryCompatible'
    ...no

Checking if Java Sound is compatible...
    ...no

It seems really strange that Java Sound wouldn't be compatible either... because it works when I add it in the SoundSystem c'tor.

I added in all the lwjgl libs too (I think), I have:
AppleJavaExtensions.jar
asm-debug-all.jar
CodecJOrbis.jar
CodevWav.jar
jinput.jar
LibraryJavaSound.jar
LibraryLWJGLOpenAL.jar
lwjgl_test.jar
lwjgl_util_applet.jar
lwjgl_util.jar
lwjgl-debug.jar
lwjgl.jar
lzma.jar
SoundSystem.jar

Code: [Select] [nofollow]
    boolean aLCompatible = SoundSystem.libraryCompatible( LibraryLWJGLOpenAL.class );
    boolean jSCompatible = SoundSystem.libraryCompatible( LibraryJavaSound.class );

Offline Paul

  • Administrator
  • double
  • *****
  • Posts: 3473
  • Developer
    • View Profile
    • PaulsCode.Com
Re: SoundSystem playbackgroundmusic null pointer
« Reply #3 on: January 31, 2014, 02:44:20 PM »
Thanks, I'll take a look.  Does LWJGL load if you skip the check for compatibility step, or no?
Device: Samsung Galaxy Nexus i515
CPU: TI OMAP4460, 1.2 GHz (dual core, ARM Cortex-A9)
GPU: PowerVR SGX540, 307 MHz
RAM: 1 GB
Resolution: 720 x 1280
Rom: omni-4.4.4-20141014-toro-FML KitKat 4.4.4, rooted

Device: Eee PC 1015PEM
CPU: Intel Atom N550, 1.5 GHz (dual core, x86)
GPU: Intel GMA 3150, 200 MHz (dual core)
RAM: 2GB
Resolution: 1024 x 600
Rom: android-x86-4.3-20130725 Jelly Bean 4.3, rooted

Offline metapod

  • bit
  • Posts: 5
    • View Profile
Re: SoundSystem playbackgroundmusic null pointer
« Reply #4 on: January 31, 2014, 03:56:42 PM »
If I skip the check and try to just construct it this happens...

Starting up SoundSystem...
Exception in thread "Thread-1" java.lang.NoClassDefFoundError: Could not initialize class org.lwjgl.openal.AL
   at paulscode.sound.libraries.LibraryLWJGLOpenAL.init(LibraryLWJGLOpenAL.java:164)
   at paulscode.sound.SoundSystem.CommandNewLibrary(SoundSystem.java:1576)
   at paulscode.sound.SoundSystem.CommandQueue(SoundSystem.java:2572)
   at paulscode.sound.CommandThread.run(CommandThread.java:121)
Initializing LWJGL OpenAL
    (The LWJGL binding of OpenAL.  For more information, see http://www.lwjgl.org [nofollow])

(I'm really not trying to be lazy by asking questions, I just keep getting stuck! I appreciate the feedback though.)

Offline metapod

  • bit
  • Posts: 5
    • View Profile
Re: SoundSystem playbackgroundmusic null pointer
« Reply #5 on: January 31, 2014, 04:59:17 PM »
Never mind I was missing some of the needed native files (.dll's in my case)!

Offline metapod

  • bit
  • Posts: 5
    • View Profile
Re: SoundSystem playbackgroundmusic null pointer
« Reply #6 on: January 31, 2014, 05:09:17 PM »
Although I'm not sure why it thinks LibraryJavaSound is not compatible, how do you add the native path for that?