1

I have the following method to play audio clips:

public static synchronized void playSound(final String path) {
      new Thread(new Runnable() {
      // The wrapper thread is unnecessary, unless it blocks on the
      // Clip finishing; see comments.
        public void run() {
            try {
                Clip clip = AudioSystem.getClip();
                AudioInputStream inputStream = AudioSystem.getAudioInputStream(
                        this.getClass().getClassLoader().getResourceAsStream(path));
                clip.open(inputStream);
                clip.loop(Clip.LOOP_CONTINUOUSLY);
                while (runThread) {
                    Thread.sleep(5000);
                }
                clip.stop();
            }
            catch (Exception e) {
                System.err.println("Audio clip error: "+e.getMessage());
                e.printStackTrace();
            }
        } 
      }).start();
    }

When I run this on a Windows 7 desktop platform (Lenovo), it works as expected. However, when run on a windows 7 laptop (Acer), I get the following exception:

java.lang.NullPointerException
at com.sun.media.sound.SoftMidiAudioFileReader.getAudioInputStream(Unknown Source)
at javax.sound.sampled.AudioSystem.getAudioInputStream(Unknown Source)
at StokerMonitor.Alerts$1.run(Alerts.java:62)
at java.lang.Thread.run(Unknown Source)

Line 62 is the 'AudioInputStream' statement. I don't know what the difference is between the 2 platforms but I am guessing there must be some Windows 7 dependency that is missing on the laptop. Does anyone have any suggestions? TIA.

Wt Riker
  • 514
  • 12
  • 24
  • `this.getClass().getClassLoader().getResourceAsStream(path)` is returning null. Verify that your .jar actually contains an entry matching that path. Note that getResourceAsStream *does not* take a file name as an argument—it takes a relative URL (with no scheme or authority), which must use forward-slashes (`/`) as directory separators on all platforms. – VGR Jun 23 '16 at 15:41
  • Its the same jar on both platforms. The variable named 'path' is a misnomer. It is just a .wav file name. – Wt Riker Jun 23 '16 at 15:45
  • What is the value of `path`? – VGR Jun 23 '16 at 15:46
  • It varies but the one I use to test is 'alarm.wav'. – Wt Riker Jun 23 '16 at 15:47
  • Add a print statement to verify that `this.getClass().getClassLoader().getResourceAsStream(path)` is returning a non-null value. And double-check the size of the .jar file, in bytes, on each machine. – VGR Jun 23 '16 at 15:51
  • I found the problem, sort of. I can't explain it but I deleted the source folder that contains the wav files from the build path and then re-added it (in Eclipse). The new jar file then worked on both. I'm thinking some kind of glitch in Eclipse. Thanks anyway. – Wt Riker Jun 23 '16 at 16:24

1 Answers1

0

It turns out that somehow the resources directory was removed from the build path in Eclipse. Sorry for the bother.

Wt Riker
  • 514
  • 12
  • 24