0

I've been trying to make a loading program for my friend. He said that it would be better if it played music, so I tried to add music and it's saying it doesn't work. It keeps on giving me an error saying The system cannot find the file specified but the file is in the same package as the class.

static File sound;
static boolean muted = false;
static float volume = 100.0f;
static float pan = 0.0f;
static double seconds = 0.0d;
static boolean loopedForever = false;
static int loopTimes = 0;
static int loopsDone = 0;

public static void main(String[] args){
    sound = new File("src/196006__corsica-s__patriceo.wav");
    new Thread(play).start();
}
final static Runnable play = new Runnable() // This Thread/Runnabe is for playing the sound
{
    public void run()
    {
        try
        {
            // Check if the audio file is a .wav file
            if (sound.getName().toLowerCase().contains(".wav"))
            {
                AudioInputStream stream = AudioSystem.getAudioInputStream(sound);

                AudioFormat format = stream.getFormat();

                if (format.getEncoding() != AudioFormat.Encoding.PCM_SIGNED)
                {
                    format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
                            format.getSampleRate(),
                            format.getSampleSizeInBits() * 2,
                            format.getChannels(),
                            format.getFrameSize() * 2,
                            format.getFrameRate(),
                            true);

                    stream = AudioSystem.getAudioInputStream(format, stream);
                }

                SourceDataLine.Info info = new DataLine.Info(
                        SourceDataLine.class,
                        stream.getFormat(),
                        (int) (stream.getFrameLength() * format.getFrameSize()));

                SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info);
                line.open(stream.getFormat());
                line.start();

                // Set Volume
                FloatControl volume_control = (FloatControl) line.getControl(FloatControl.Type.MASTER_GAIN);
                volume_control.setValue((float) (Math.log(volume / 100.0f) / Math.log(10.0f) * 20.0f));

                // Mute
                BooleanControl mute_control = (BooleanControl) line.getControl(BooleanControl.Type.MUTE);
                mute_control.setValue(muted);

                FloatControl pan_control = (FloatControl) line.getControl(FloatControl.Type.PAN);
                pan_control.setValue(pan);

                long last_update = System.currentTimeMillis();
                double since_last_update = (System.currentTimeMillis() - last_update) / 1000.0d;

                // Wait the amount of seconds set before continuing
                while (since_last_update < seconds)
                {
                    since_last_update = (System.currentTimeMillis() - last_update) / 1000.0d;
                }

                System.out.println("Playing!");

                int num_read = 0;
                byte[] buf = new byte[line.getBufferSize()];

                while ((num_read = stream.read(buf, 0, buf.length)) >= 0)
                {
                    int offset = 0;

                    while (offset < num_read)
                    {
                        offset += line.write(buf, offset, num_read - offset);
                    }
                }

                line.drain();
                line.stop();

                if (loopedForever)
                {
                    new Thread(play).start();
                }
                else if (loopsDone < loopTimes)
                {
                    loopsDone++;
                    new Thread(play).start();
                }
            }
        }
        catch (Exception ex) {ex.printStackTrace();} 

    }
};
Community
  • 1
  • 1
Anu
  • 13
  • 4
  • The answer might be here: http://stackoverflow.com/questions/16570523/getresourceasstream-returns-null – Pétur Ingi Egilsson Dec 23 '15 at 14:20
  • Try to move your file out of the package and into the source folder. If your trouble is with loading a file, don't paste so much code, 10 relevant lines are more than enough. – user1803551 Dec 23 '15 at 14:22
  • You have 2 ways you're trying to access the .wav file: `sound = new File("196006__corsica-s__patriceo.wav");` and `InputStream is = getClass().getClassLoader().getResourceAsStream("196006_corsica-s_patriceo.wav");` Which line is throwing the error? – Clark Kent Dec 23 '15 at 14:26
  • I switched the .wav file to the source folder and deleted the InputStream, now it gives me this error `java.lang.IllegalArgumentException: No line matching interface SourceDataLine supporting format PCM_SIGNED 96000.0 Hz, 24 bit, mono, 3 bytes/frame, little-endian, and buffers of 1984563 to 1984563 bytes is supported. at javax.sound.sampled.AudioSystem.getLine(Unknown Source) at main.loading$2.run(loading.java:108) at java.lang.Thread.run(Unknown Source)` – Anu Dec 23 '15 at 14:34
  • Post an [MCVE](http://stackoverflow.com/help/mcve). Be sure to copy-paste your code to a *new project* and make sure it compiles and runs before posting it here. – user1803551 Dec 23 '15 at 14:37
  • 1
    I removed the code that works and left the parts that don't work – Anu Dec 23 '15 at 14:47

1 Answers1

1

I tested your code, it works fine. You problem is with the location of the file in the folder/package hierarchy.

This is how I access the file based on your code:

public class Example {

    public static void main(String[] args) {

        sound = new File("harpsi-cs.wav");
        new Thread(play).start();
    }
}

and this is my hierarchy:

enter image description here

Often projects create a resources folder to put their files there. In that case, don't forget to make it a source folder and change your path accordingly:

sound = new File("resources/harpsi-cs.wav");

enter image description here

user1803551
  • 12,965
  • 5
  • 47
  • 74
  • when you ran the code did you change the format, because when i tried to run it, it gives me this error: `java.lang.IllegalArgumentException: No line matching interface SourceDataLine supporting format PCM_SIGNED 96000.0 Hz, 24 bit, mono, 3 bytes/frame, little-endian, and buffers of 1984563 to 1984563 bytes is supported.` – Anu Dec 23 '15 at 15:19
  • @Gray Changed the format of what? The file is a `.wav` file. – user1803551 Dec 23 '15 at 15:20
  • this is a youtube video of the file i'm using https://www.youtube.com/watch?v=wvHyfsG6N6A&feature=youtu.be – Anu Dec 23 '15 at 15:36
  • @Gray But does it work with my file? If it does then the problem is with your file itself. To test yours, I need the `.wav` file, not a video. – user1803551 Dec 23 '15 at 15:39
  • here's a mediafire link http://www.mediafire.com/listen/lq1cr64ca262q2x/196006__corsica-s__patriceo.wav – Anu Dec 23 '15 at 15:45
  • @Gray Your file is damaged. My media players won't play it either. – user1803551 Dec 23 '15 at 15:56
  • thanks a lot, i was able to download the youtube video as a .wav file and used it in the program. – Anu Dec 23 '15 at 16:03