Can not find audio within src

1

I have an "audios" folder inside "src", so when I build the project I can play the audios. I try to open the audio but I skip "java.lang.NullPointerException"

    public void playFalloVibrador1Reponer() {
    try {
        InputStream path;
        path = getClass().getResourceAsStream("/audios/vibradores/reponer/1.wav");
        s1v1 = AudioSystem.getClip();
        s2v1.open(AudioSystem.getAudioInputStream(path));

        s1v1.start();
        sleepThread();//espera 3 segundos para pode reproducir el audio
        s1v1.close();
    } catch (Exception ex) {
        System.err.println(" " + ex);
    }
}
    
asked by R.Priego 26.10.2016 в 13:44
source

3 answers

1

A good way to check if the route is good is what you have written in the comments, apart from that, you should eliminate the getClass() as indicated in the next answer from OS in English.

    
answered by 26.10.2016 в 14:25
1

The problem is that when you generate the compilations of your Java application, your audio files are not being included. To facilitate this, the folder where you have your resources (audio files and others) should be configured as a folder of project sources. Once that is set, you can access these files with the code you show in your question.

If you use maven, this is easier since there is a src / main / resources folder. Within that folder, you can place all files that are not Java classes, which are resources of the application, such as your audio files, configuration files, among others.

    
answered by 26.10.2016 в 18:16
1

It's simple you have to put a resource tag in your maven application so that when you generate the application include these audios to the jar, war or whatever you are doing.

<build> 
 <resources> 
  <resource> 
   <directory>src/main/java</directory> 
   <includes> 
    <include>**/*.xml</include> </includes> 
  </resource> 
 </resources>
 ....
    
answered by 27.10.2016 в 21:20