3

I have looked at countless different StackOverflow answers as well as answers from other sites, but none of the solutions have fixed my problem. I cannot for the life of me get my .wav file to play.

Here is my code:

Sound class:

public class Sound {
/**
 * Static file paths for each sound.
 */
public static String stepSound = "/resources/step.wav";

/**
 * Audio input stream for this sound.
 */
private AudioInputStream audioInputStream;

/**
 * Audio clip for this sound.
 */
private Clip clip;

/* -- Constructor -- */

/**
 * Creates a new sound at the specified file path.
 * 
 * @param   path    File path to sound file
 */
public Sound(String path) {
    // Get the audio from the file
    try {
        // Convert the file path string to a URL
        URL sound = getClass().getResource(path);
        System.out.println(sound);

        // Get audio input stream from the file
        audioInputStream = AudioSystem.getAudioInputStream(sound);

        // Get clip resource
        clip = AudioSystem.getClip();

        // Open clip from audio input stream
        clip.open(audioInputStream);
    } catch (UnsupportedAudioFileException | IOException | LineUnavailableException e) {
        e.printStackTrace();
    }
}

/* -- Method -- */

/**
 * Play the sound.
 */
public void play() {
    // Stop clip if it's already running
    if (clip.isRunning())
        stop();

    // Rewind clip to beginning
    clip.setFramePosition(0);

    // Play clip
    clip.start();
}

/**
 * Stop the sound.
 */
public void stop() {
    clip.stop();
}
}

Constructor call that leads to error:

// Play step sound
new Sound(Sound.stepSound).play();

I know this isn't the first time a problem like this has been asked or answered on this website, but I've been trying other solutions for hours at this point and all I've found is pain and frustration. I can post more code if needed. Thanks in advance.

EDIT: I have unpacked the .jar file and confirmed that the file is indeed there. The problem is that the URL ends up being null, and so a NullPointerException is thrown.

EDIT #2: Added more code in case there's another problem.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Remove `\resource` from the path. – Yahya Jun 03 '17 at 22:42
  • @Yahya I assume you mean change the line `public static String stepSound = "/resources/step.wav";` to `public static String stepSound = "/step.wav";`, which still results in the `NullPointerException`. – elektrikpulse61 Jun 03 '17 at 22:48
  • Is `resources` a Package or a resource folder in the project ? – Yahya Jun 03 '17 at 22:49
  • And what `System.out.println(sound);` prints? – Yahya Jun 03 '17 at 22:52
  • @Yahya `resources` is a package -- a folder under the `src` folder. It's the only way I could get the images to load. Also, `System.out.println(sound);` prints null, hence the `NullPointerException`. – elektrikpulse61 Jun 03 '17 at 22:54
  • So try `Sound.class.getResource(path)` – Yahya Jun 03 '17 at 22:58
  • @Yahya That works in Eclipse IDE, but it still prints `null` and throws a `NullPointerException` when I run the .jar file through the command line. – elektrikpulse61 Jun 03 '17 at 23:03
  • Try `InputStream is= getClass().getResourceAsStream("/resources/step.wav");` then `audioInputStream = AudioSystem.getAudioInputStream(new BufferedInputStream(is));` – Yahya Jun 03 '17 at 23:12
  • and don't forget `clip.start();` – Yahya Jun 03 '17 at 23:16
  • @Yahya When I run the `println(is)`, I get `null` and then it throws an `IOException` at `audioInputStream = AudioSystem.getAudioInputStream(new BufferedInputStream(is));` Once again this works in Eclipse, but fails when I run it as a .jar file. – elektrikpulse61 Jun 03 '17 at 23:19
  • @Yahya I put `clip.start()` in a different method. I only included the erroneous part of the code in this post. – elektrikpulse61 Jun 03 '17 at 23:22
  • That's weird, I tried `InputStream is= Sound.class.getResourceAsStream("/resources/step.wav");` and it's working! – Yahya Jun 03 '17 at 23:23
  • @Yahya And this is through the console as a .jar file? I ask because I have found numerous ways to get sound in the Eclipse IDE runtime, but I get no sound in the .jar file format. – elektrikpulse61 Jun 03 '17 at 23:27
  • Yes this to play it from the jar file! Are you sure that `clip.start()` is invoked properly? – Yahya Jun 03 '17 at 23:29
  • @Yahya I will edit my original post to include more code. Perhaps there is something else going on here. – elektrikpulse61 Jun 03 '17 at 23:30
  • Please include enough code , so I can copy-paste it to my IDE to verify. – Yahya Jun 03 '17 at 23:31
  • I tried your code , you're missing to sleep the thread! – Yahya Jun 03 '17 at 23:43
  • 1
    @Yahya You're never going to believe it, but there was nothing wrong with the `URL` or the `InputStream` or any of that... it was that I had my file named `Step.wav`, and in my String file path, I put `step.wav` (notice the lowercase 's'). All along it was just a capitalization error... I'm sorry for wasting your time. Thank you so much for your help. This is beyond embarrassing for me. – elektrikpulse61 Jun 03 '17 at 23:44
  • haha no worries man :) – Yahya Jun 03 '17 at 23:45
  • But out of curiosity, how could you play the entire sound without sleeping the thread ? – Yahya Jun 03 '17 at 23:46
  • @Yahya The method I'm using has its own thread, so there's no need to sleep the main thread. Something with the `Clip` class I'd assume or maybe the `AudioSystem` class. – elektrikpulse61 Jun 03 '17 at 23:52
  • Cool, I am glad that you could fix it :) – Yahya Jun 03 '17 at 23:54

0 Answers0