-2
 public static synchronized void playSound(final String url) {
    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(
                        Main.class.getResourceAsStream("Draco-s-Pong-master\\demo\\Sounds" + url));
                clip.open(inputStream);
                clip.start();
            } catch (Exception e) {
                System.err.println(e.getMessage());
            }
        }
    }).start();
    playSound("Draco Background.wav");
}

I have looked dozens of codes from other threads and they all give me null and keep spamming it, until i close the program.I have that .wav file in the Sounds folder, i even placed it everywhere in the project and it still gives me null every time.I want it for simple background.

Pafo
  • 29
  • 4
  • Main.class.getResourceAsStream("Draco-s-Pong-master\\demo\\Sounds" + url)) change to Main.class.getResourceAsStream("//Sounds" + url)) – andy Apr 16 '16 at 08:48
  • can you explain it simpler, what do you mean by folder under my classpath, how to understand my classpath? – Pafo Apr 16 '16 at 08:50
  • The classpath is the root of your file structure, the folder which is the default package. See [link](http://docs.oracle.com/javase/7/docs/technotes/tools/windows/classpath.html) – Jorn Vernee Apr 16 '16 at 08:53
  • I still do not fully understand, how to check my classpath? – Pafo Apr 16 '16 at 08:59

1 Answers1

1

Move your audio files (in your case the sound folder) to your class folder and then simply call it with the updated path. As

Main.class.getResourceAsStream("*PUT YOUR CLASS PATHNAME*\\Sounds" + url))

EDIT:

Use only the absolute path to the audio file. Leave off the whole Main.class.getResourceAsStream and use

try {
    String fileName = "..add the rest of the absolute path..\\Draco-s-Pong-master\\demo\\Sounds\\Draco Background.wav";
    File file = new File(fileName);
    if (file.exists()) {
        AudioInputStream inputStream = AudioSystem.getAudioInputStream(file);
        Clip clip = AudioSystem.getClip();
        clip.open(inputStream);
        clip.setFramePosition(0); // to start from the beginning
        clip.start();
    } else {
           throw new RuntimeException("Sound: file not found: " + fileName);
    }
} catch(Exception e){
                stopPlay();
                System.err.println(e.printStackTrace());
}

So now you will know if the problem is with the file giving the runtime exception. Otherwise it is somewhere else.

Dimitar
  • 4,402
  • 4
  • 31
  • 47
  • How to check what is my class pathname i really don't get the idea of that? – Pafo Apr 16 '16 at 09:15
  • that is the directory where your class is. – Dimitar Apr 16 '16 at 09:24
  • i am using Intelij , C:\Users\Pafo37\Downloads\Draco-s-Pong-master\Draco-s-Pong-master\demo\src\pong here are all my .java files . – Pafo Apr 16 '16 at 09:27
  • that should do it? – Dimitar Apr 16 '16 at 09:32
  • still getting null – Pafo Apr 16 '16 at 09:36
  • have you moved your `Sounds` folder too? – Dimitar Apr 16 '16 at 09:40
  • yes i did, still null.Btw when the .wav appears left in Intelij, it has a ? before it.The program doesn't recognize the type of file. – Pafo Apr 16 '16 at 09:42
  • the easiest but not much portable solution would be to put them in your project directory and then call them by file-name. In other words using a relative path. And also if you used the `Sounds`-folder call it in `playSound("Sounds\\Draco Background.wav")` – Dimitar Apr 16 '16 at 09:52
  • Unfortunately it is still null... :( – Pafo Apr 16 '16 at 09:57
  • well, I haven't worked with Intelij but if the file is not recognized see: http://stackoverflow.com/questions/15915202/intellij-not-recognizing-a-particular-file-correctly-instead-its-stuck-as-a-tex/15918460#15918460 – Dimitar Apr 16 '16 at 10:00
  • I have opened another project, which is still not recognized and it runs without a problem, i even copied the code, placed the files the exact same way and i still have a problem. – Pafo Apr 16 '16 at 10:04
  • Nothing changed, still the same . – Pafo Apr 16 '16 at 10:20
  • Now it should tell you whether or not that is the problem. – Dimitar Apr 16 '16 at 11:05
  • I did that thing from the link, nothing happened.How this should tell me what is the problem? – Pafo Apr 16 '16 at 11:18