0

I'm trying to make a very simple program to play a sound file.

So far all I have is:

import java.io.File;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;

public class SoundTest {

    public static void main(String[] args) {

        File sound = new File("/home/pierce/Downloads/clapping.wav");
        playSound(sound);

    }

    static void playSound(File sound) {
        try {
            Clip clip = AudioSystem.getClip();
            clip.open(AudioSystem.getAudioInputStream(sound));
        } catch(Exception e) {
            System.out.println("Something failed");
        }
    }

}

After adding the line "clip.open(AudioSystem.getAudioInputStream(sound));", I started getting the message in the exception. Basically I have no idea why. Any help would be appreciated.

If it helps to see what I'm aiming at, I'm trying to follow this tutorial.

Thanks

Edit: Stack trace, as requested:

java.lang.IllegalArgumentException: Invalid format
    at org.classpath.icedtea.pulseaudio.PulseAudioDataLine.createStream(PulseAudioDataLine.java:142)
    at org.classpath.icedtea.pulseaudio.PulseAudioDataLine.open(PulseAudioDataLine.java:99)
    at org.classpath.icedtea.pulseaudio.PulseAudioDataLine.open(PulseAudioDataLine.java:283)
    at org.classpath.icedtea.pulseaudio.PulseAudioClip.open(PulseAudioClip.java:402)
    at org.classpath.icedtea.pulseaudio.PulseAudioClip.open(PulseAudioClip.java:453)
    at SoundTest.playSound(SoundTest.java:17)
    at SoundTest.main(SoundTest.java:10)
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
P.Smith
  • 1
  • 2
  • 2
    Put `e.printStackTrace()` in the catch block and edit the question to add the stack trace. The exception tells you what the problem is. – Radiodef Jun 04 '18 at 15:08
  • @Radiodef Good point. Trace added by OP: `IllegalArgumentException` **OP:** See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) – Andrew Thompson Jun 04 '18 at 15:40
  • 2
    One thing would be to check what the actual format of the file is, since the API implementation isn't giving us a good error message. I poked around, though, and found this: https://stackoverflow.com/questions/21128797/audioinputstream-is-not-working. If the answer in that Q&A solves the issue, then I guess this could be an issue with how the Pulse Audio API creates a Clip. – Radiodef Jun 04 '18 at 15:44
  • It is almost certainly a mismatch between the format of the source audio and the default format Java is attempting to use. To get the format of the audio (and improve this question), please check the properties (right-click on file, select properties) and within that the details and report back. Will probably have to make an explicit Format to accommodate. Another possibility is to use Audacity to change the format to whatever the default is (have to inspect the Format). I've seen Pulse Audio before, I think it is just one of a number of normal audio signal providers, shouldn't be an issue. – Phil Freihofner Jun 05 '18 at 16:58

0 Answers0