I am currently having trouble playing around with the javax.sound.sampled
library. Here\'s the MCV code that I use to play my audio files:
import ja
You can use the following code to get a list of the supported formats for playback:
public static List getSupportedAudioFormats() {
List result = new ArrayList<>();
for (Line.Info info : AudioSystem.getSourceLineInfo(
new Line.Info(SourceDataLine.class))) {
if (info instanceof SourceDataLine.Info) {
Collections.addAll(result, ((SourceDataLine.Info) info).getFormats());
}
}
return result;
}
AudioFormat.Encoding lists the encodings supported by javax.sound.sampled
.
A safe WAV audio format is 16-bit PCM at 44100Hz.
You can discover the format of a particular file with:
File file = new File("path_to_file.wav");
AudioFormat fmt = AudioSystem.getAudioFileFormat(file).getFormat();
This will be a little more lenient than trying to get a line for example, but it will still throw if the WAV file has e.g. mp3 data. A WAV file is a container that can store encodings beyond PCM, some of which javax.sound.sampled
does not normally support.