0

I've looked at different solutions at Stackoverflow to play a sound in Java. The example given below (from another topic) is the one I would like to use.

import java.io.*;
import java.net.URL;
import javax.sound.sampled.*;
import javax.swing.*;

// To play sound using Clip, the process need to be alive.
// Hence, we use a Swing application.
public class SoundClipTest extends JFrame {

   public SoundClipTest() {
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      this.setTitle("Test Sound Clip");
      this.setSize(300, 200);
      this.setVisible(true);

      try {
         // Open an audio input stream.
         URL url = this.getClass().getClassLoader().getResource("gameover.wav");
         AudioInputStream audioIn = AudioSystem.getAudioInputStream(url);
         // Get a sound clip resource.
         Clip clip = AudioSystem.getClip();
         // Open audio clip and load samples from the audio input stream.
         clip.open(audioIn);
         clip.start();
      } catch (UnsupportedAudioFileException e) {
         e.printStackTrace();
      } catch (IOException e) {
         e.printStackTrace();
      } catch (LineUnavailableException e) {
         e.printStackTrace();
      }
   }

   public static void main(String[] args) {
      new SoundClipTest();
   }
}

But how is it possible to get the file from src folder (it's already in my src)? Or is there another code solution to play a .wav file from src/... folder?

Thanks in advance!

Joost
  • 42
  • 7

1 Answers1

1

This code

this.getClass().getClassLoader().getResource(...) 

will fetch a resource relative to where classes are lodaed from. The runtime doesn't know anything about sources. If you move your file to compiled classes folder, this should work.

pafau k.
  • 1,667
  • 12
  • 20
  • Thanks for answering my question. Could you explain me how to come there? – Joost May 19 '20 at 20:58
  • Ok, so *source* folder is the location where your SoundClipTest.java file is. Analogously *classes* folder is where, after compilation, your SoundClipTest.class file lands. You should check where your IDE compiles your files to, or compile and use your operating system's search option to find the location of the file. – pafau k. May 19 '20 at 21:10
  • It's an example from another topic, so I don't have a SoundClipTest.java/class. But it makes no sense. However, I've found the folder with .class files. Should i put the file in this folder? – Joost May 19 '20 at 21:25
  • @Joost Make the resources folder and place the sound file there. Configure this folder as the resources root in IntelliJ IDEA, see https://www.jetbrains.com/help/idea/configuring-content-roots.html. When you Build | Build the project, the sound file will be placed into classpath (output directory) automatically. – CrazyCoder May 19 '20 at 21:32
  • I have added it to the correct folder now, but it still doesn't work – Joost May 19 '20 at 21:39
  • 1
    Please share the [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). – CrazyCoder May 19 '20 at 21:40
  • I get an error (nullPointerException) at line "AudioInputStream audioIn = AudioSystem.getAudioInputStream(url)" – Joost May 19 '20 at 21:46
  • Zip and share the project directory using any file sharing service. – CrazyCoder May 19 '20 at 21:49