Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Sound API

 
George Krueger
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Has anybody else had trouble with getting certain sound formats to play while using the javax.sound.sampled package.
For example, I can play a sound of the format:
PCM_SIGNED, 11025.0 Hz, 16 bit, mono, little-endian, audio data
But I can't play a sound of the format:
PCM_SIGNED, 44100.0 Hz, 16 bit, stereo, little-endian, audio data
I've checked the Java Sound API website several times, and according to it this sound file should be supported. It says:
"The Java Sound engine can render 8 or 16 bit audio data, in mono or stereo, with sample rates from 8KHz to 48KHz".
I've tried using the isFormatSupported() function to make sure this file format is supported and it returns true. So I really don't know what the problem could be.
Any thoughts?
I've also included my code below.

try {
AudioInputStream stream = (AudioInputStream) currentSound;
AudioFormat format = stream.getFormat();
System.out.println(format.toString());

/**
* we can't yet open the device for ALAW/ULAW playback,
* convert ALAW/ULAW to PCM
*/
if ((format.getEncoding() == AudioFormat.Encoding.ULAW) | |
(format.getEncoding() == AudioFormat.Encoding.ALAW))
{
AudioFormat tmp = new AudioFormat(
AudioFormat.Encoding.PCM_SIGNED,
format.getSampleRate(),
format.getSampleSizeInBits() * 2,
format.getChannels(),
format.getFrameSize() * 2,
format.getFrameRate(),
true);
stream = AudioSystem.getAudioInputStream(tmp, stream);
format = tmp;
}
DataLine.Info info = new DataLine.Info(
Clip.class,
stream.getFormat(),
((int) stream.getFrameLength() *
format.getFrameSize()));
System.out.println(info.isFormatSupported(format));
Clip clip = (Clip) AudioSystem.getLine(info);
clip.open(stream);
currentSound = clip;
}
catch (Exception ex) {
System.out.println("Could not obtain dataline");
ex.printStackTrace();
}
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic