1

Im trying to make sound effects for my game but i couldn't achieve to play it yet. I have looked up everywhere but everytime that i try to do what i see i always have this error:

javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input file
at javax.sound.sampled.AudioSystem.getAudioInputStream(Unknown Source)
at pong.Pong.loadResources(Pong.java:114)
at pong.Pong.<init>(Pong.java:69)
at pong.Pong.main(Pong.java:34)

This the code that i try to run:

try {   
        AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File("score.wav"));
        AudioFormat format = audioInputStream.getFormat();
        DataLine.Info info = new DataLine.Info(Clip.class, format);
        clip = (Clip) AudioSystem.getLine(info);
        clip.open(audioInputStream);
        clip.start();

    } catch (UnsupportedAudioFileException | IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (LineUnavailableException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

Just to be sure, i placed the same named same wav file under directly the project folder, src folder, res folder too. I would like to have it opened from res folder by the way but first thing i have to do is just fixing the code to play the sound. Then i can work with the path declaration. Any help would be appreciated!

Tolga Şen
  • 289
  • 5
  • 14
  • 1
    Try putting in `System.out.println(new File("score.wav").exits());` into your code, I suspect the file does not exist where you think it does – MadProgrammer Feb 20 '16 at 20:29

1 Answers1

0

Try something like this:

    File file = new File("clip.wav");
    if(file.exists()) {
        try {
            AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(file);

            AudioFormat audioFormat = audioInputStream.getFormat();

            DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);

            SourceDataLine sourceLine = (SourceDataLine) AudioSystem.getLine(info);
            sourceLine.open(audioFormat);

            sourceLine.start();

            int nBytesRead = 0;
            byte[] abData = new byte[128000];
            while (nBytesRead != -1) {
                try {
                    nBytesRead = audioInputStream.read(abData, 0, abData.length);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (nBytesRead >= 0) {
                    sourceLine.write(abData, 0, nBytesRead);
                }
            }

            sourceLine.drain();
            sourceLine.close();

        } catch (UnsupportedAudioFileException | IOException e) {
            e.printStackTrace();
        } catch (LineUnavailableException e) {
            e.printStackTrace();
        }
    } else {
        System.err.println("The selected file doesn't exist!");
    }

It works fine. If you are using Eclipse (I don't know how it works with others IDEs) make sure you place the file in the project's root folder.

Solution from this question.

Community
  • 1
  • 1
aleb2000
  • 452
  • 4
  • 10
  • I have tried that solution too and it's not working, still i get the same error exception throw – Tolga Şen Feb 20 '16 at 23:46
  • Which operative system do you use and which version of java? – aleb2000 Feb 21 '16 at 10:04
  • @TolgaŞen Also try with other wav files, maybe your file is corrupted. – aleb2000 Feb 21 '16 at 10:09
  • I use windows8.1 and java7, when i try with the other files it still throws the same error. I suppose nothing wrong with the file – Tolga Şen Feb 21 '16 at 11:16
  • I'm using "java8 update 73" and the code works fine, I don't know but maybe it's a bug of your java version, try to update java if you can. – aleb2000 Feb 21 '16 at 12:06
  • I have updated java to 1.8 also and its still throwing the same error. Do we have another way to do this? I mean playing a sound shouldn't be this complicated... – Tolga Şen Feb 21 '16 at 18:47