开发者

How to play different sounds in Java?

开发者 https://www.devze.com 2023-03-09 16:37 出处:网络
I am trying to play sounds in java... private Clip clip; public Sound(String filename) { try{ AudioInputStream ais;

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.o开发者_开发知识库pen(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


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);


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消