4

I am trying to play sounds in java...

private Clip clip;

public Sound(String filename)
{
    try{
        AudioInputStream ais;
        ais = AudioSystem.getAudioInputStream(this.getClass().getResource(filename));
        clip = AudioSystem.getClip();
        clip.open(ais);
    }catch(Exception e){e.printStackTrace();}

}
public void playSFX()
{
    clip.stop();
    clip.setFramePosition(0);
    clip.start();
}

I use the above code with .wav files. I can successfully play certain .wav clips; however I cannot play other .wav clips. What am I doing wrong? Also to be noted: I wish to play brief (< 3 seconds) sound effects. I get the UnsupportedAudioFileException for the certain clips that do not play (they are .wav as well). Sample unworking clip: link Sample working clip: link

Derek
  • 41
  • 2
  • 1
    Maybe you can provide sample links to files that work and don't work? – ashes999 May 30 '11 at 00:47
  • 2
    @ashes999: +1 WAV is a container format that might have any of a number of internal encodings. Java Sound will play some of those encodings, but not others. @Derek: If this is an applet, use `URL` (exclusively) instead of `File`. If this is not an applet, use `Clip` instead of `AudioClip`. – Andrew Thompson May 30 '11 at 01:04
  • Ok, I adjusted my program to use Clips instead, and I get the error: UnsupportedAudioFileException. I used the answer in this: [link](http://stackoverflow.com/questions/2246809/how-to-play-sounds-in-java-games) to implement the Clip. – Derek May 30 '11 at 01:58
  • Have a look at this question, he has a similar problem: I notice someone in one of the answers mentiones a method `waitUntilDone()` http://stackoverflow.com/questions/577724/trouble-playing-wav-in-java – javing Jun 06 '11 at 08:58
  • BTW - both those links are redirecting me to an amazon.com page for a 'Kindle'. What's the deal with that? – Andrew Thompson Jun 07 '11 at 06:58

2 Answers2

1

As previous folks are saying, some WAV formats are not supported. I'll just add a bit more detail.

I often run into WAVs that are encoded at 24-bits or 32-bits, when 16-bits is the maximum that javax.sound.sampled supports.

To find out about a particular .wav file, if you have Windows, you can right click the file and check the properties, and "summary" tab. I don't know what the equivalent is on a MAC or Linux system.

Once you know the format, you can check if it is supported with code in this tutorial: http://download.oracle.com/javase/tutorial/sound/converters.html See the discussion in "Writing Sound Files" where they introduce the AudioSystem method, "isFileTypeSupported".

Here is a list of the formats that are supported on my PC. I got this list by inspecting a LineInfo object via the Eclipse debugger. I suspect these are standard, but I'm not sure:

BigEndian = false, PCM_UNSIGNED, channels = 1, bits = 8
BigEndian = false, PCM_SIGNED, channels = 1, bits = 8
BigEndian = false, PCM_SIGNED, channels = 1, bits = 16
BigEndian = true, PCM_SIGNED, channels = 1, bits = 16
BigEndian = false, PCM_UNSIGNED, channels = 2, bits = 8
BigEndian = false, PCM_SIGNED, channels = 2, bits = 8
BigEndian = false, PCM_SIGNED, channels = 2, bits = 16
BigEndian = true, PCM_SIGNED, channels = 2, bits = 16

Most WAV files that I work with are the second to the last in the above list: little endian, 16-bit, PCM_SIGNED, stereo, encoded at 44100 fps.

The following code might help you figure out the format of your .wav files, as well.

InputStream inStream =  YourClass.class.getResourceAsStream("YourSound.wav");
AudioInputStream aiStream = AudioSystem.getAudioInputStream(inStream);
AudioFormat audioFmt = aiStream.getFormat();            
DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFmt);
System.out.println(info);
Phil Freihofner
  • 7,645
  • 1
  • 20
  • 41
0

As far as I know, some formats are just not supported. Please, check what are the formats of those WAVs which work and which does not.

By format I mean something like here: http://en.wikipedia.org/wiki/WAV#WAV_file_compression_codecs_compared

Then you can just convert to format which works.

firen
  • 1,384
  • 2
  • 14
  • 32