-6

my java progeam run 100% if i run it from the compiler netbeans put if i run it from file manager its will be in dist folder inside the project (the program have a button that run wav file) the button have a catch ioexception if i run it from compiler the sound will run put if i run it from dist folder or move then run it to any path the catch that have the ioexception as a parameter will excecute i used audioSystem and cought it with a Clip class so if any exception occure in the first way the Clip class will try to run the sound file. code:

public void getSoundData(int delay){
  ActionListener taskPerformer = new ActionListener(){
    @Override
    public void actionPerformed(ActionEvent evt){
      try{
        int random=(int)(Math.random()*12+1);
        InputStream input = new FileInputStream("src/azcar/sounds/"+String.valueOf(random)+".wav");
        AudioStream audio = new AudioStream(input);
        AudioPlayer.player.start(audio);
      } catch (Exception exc) {
        try {
          Clip clip = AudioSystem.getClip();
          int random=(int)(Math.random()*12+1);
          AudioInputStream input = AudioSystem.getAudioInputStream(new File("src/azcar/sounds/"+String.valueOf(random)+".wav"));
          clip.open(input);
          clip.start();
        } catch (LineUnavailableException ex) {
          JOptionPane.showMessageDialog(null, ex);
        } catch (UnsupportedAudioFileException ex) {
          JOptionPane.showMessageDialog(null, "نوع ملفات الصوت غير مدعوم");
        } catch (IOException ex) {
          JOptionPane.showMessageDialog(null, "لم أتمكن من االوصول لملفات الصوت");
        }
      }
    }
  };
  Timer timer = new Timer(delay*1000*60,taskPerformer);
  timer.setRepeats(true);
  timer.start();
}
Thomas Kläger
  • 17,754
  • 3
  • 23
  • 34
  • Not sure what the question is here, perhaps elaborate on your solution and share code for someone to better understand the question? – Kevin Bayes Feb 11 '17 at 08:59
  • the paths you use are relative, if there is no "src/azcar/sounds" folder where you move your executable, the program won't find the file you want – Theofilos Mouratidis Feb 11 '17 at 12:49

1 Answers1

0

In the above you are using the relative path to look up you wav file. From the root folder of the project you will read 'src/azcar/sounds/.wav', but from the dist folder you are effectively reading from 'dist/src/azcar/sounds/.wav'.

You can start to test this by entering the absolute path to the folder as a first step. After that you can then move to passing in a sound folder path as a variable to your application.

Kevin Bayes
  • 846
  • 11
  • 17