Problem with game sound class wrong path

this is my sound class


package com.kiko.quest.sound;

import java.io.File;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.FloatControl;

public class Sound {
   private Clip clip;
   private boolean play = false;
   private String url;

   public static Sound bg1 = new Sound("/res/sounds/Fire magic.wav");

   private Sound(String url) {
      this.url = url;

   }

   public void Play(float volume, boolean repeat) {
      try {

         AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File(url));
         Clip clip = AudioSystem.getClip();
         this.clip = clip;
         clip.open(audioInputStream);
         FloatControl gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
         gainControl.setValue(volume);

      } catch (Exception ex) {
         System.err.println("Wrong sound path!");
      }

      if (repeat)
         clip.loop(Clip.LOOP_CONTINUOUSLY);
      else
         clip.start();
   }

   public void Stop() {
      clip.loop(0);
      clip.stop();
   }

}


In my game class I write Sound.bg1.Play(0, true);
I always get Wrong path! I have set the res folder.It says that clip = null everytime, I tried ex.printStackTrace(); but still same the res folder its added in the library other thinks like pictures works fine but the sound no please help

Is it a file on your system or a ressource file inside you .jar?
If it is a file (which is what it looks like) you will probably never use ‘/’ in front of a path because it would be an absolute path.
If you are using a ressource, you cannot use the File class.

wow thx a lot a removed the / and it worked ;D

Line 25 in your posted code.


@@AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File(url));

You cannot use [icode]File[/icode] from a JAR file, maybe this is the error.

@SHC: He said it worked, didn’t he?

@Drenius

Oops, I didn’t read all the replies. Sorry.

Try doing not “/res/sounds/…” but “/sounds/…”

@trollwarrior1

You did the same mistake I’ve did. OP said that he had solved the issue.