public class TestRAF { public static void main(String[] args) throws FileNotFoundException, IOException { try { File soundFile = new File("SoundAlert.wav"); //Звуковой файл System.out.println(soundFile.getAbsoluteFile()); //Получаем AudioInputStream //Вот тут могут полететь IOException и UnsupportedAudioFileException AudioInputStream ais = AudioSystem.getAudioInputStream(soundFile); //Загружаем наш звуковой поток в Clip //Может выкинуть IOException и LineUnavailableException try ( //Получаем реализацию интерфейса Clip //Может выкинуть LineUnavailableException Clip clip = AudioSystem.getClip()) { //Загружаем наш звуковой поток в Clip //Может выкинуть IOException и LineUnavailableException clip.open(ais); clip.setFramePosition(0); //устанавливаем указатель на старт clip.start(); //Поехали!!! //Если не запущено других потоков, то стоит подождать, пока клип не закончится //В GUI-приложениях следующие 3 строчки не понадобятся Thread.sleep(clip.getMicrosecondLength() / 1000); clip.stop(); //Останавливаем clip.close(); //Закрываем } } catch (IOException | UnsupportedAudioFileException | LineUnavailableException | InterruptedException exc) { System.out.println(exc); } } 

}

As a matter of fact, like this File soundFile = new File("SoundAlert.wav"); - the file is not visible, but when I insert the full path, the file is visible. C: \ Users \ User \ Documents \ NetBeansProjects \ testRAF \ src \ testraf \ SoundAlert.wav

System.out.println(soundFile.getAbsoluteFile()); - output: C: \ Users \ User \ Documents \ NetBeansProjects \ testRAF \ SoundAlert.wav Here is a screenshot of the project directory: http://joxi.ru/Dr8EBbYike8Gem What is the reason?

  • one
    the relative path is calculated from the working directory of the program (the project folder in your case), and your file is in the package. - zRrr
  • Yeah, but how to do then right? - Prototype
  • one
    How, then, does it work here? habrahabr.ru/post/191422 - Prototype

1 answer 1

For example, you can place the file in the required directory C:\Users\Пользователь\Documents\NetBeansProjects\testRAF\src\testraf\dist\SoundAlert.wav ie where is the java file

  • And how will it be launched later when the jar file is compiled? - Prototype
  • @Prototype, so it will not be collected in the jar file. You will need to carry it separately. Now, if you had picked it up as a resource in the project, then it is quite another thing. - ArchDemon pm
  • This is exactly what I need. How to make it so that he was in the project was constantly and correctly read? - Prototype
  • @Prototype, but your question is completely asking for something else. There is a rule: one question for one topic - ArchDemon