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 javax.sound.sampled.*;
import java.io.File;
public class Foo
{
public static void main(String[] args)
{
try
{
File f = new File("alarm.wav");
AudioInputStream ais = AudioSystem.getAudioInputStream(f);
Clip clip = AudioSystem.getClip();
clip.open(ais);
clip.start();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
This code will sometimes throw an UnsupportedAudioFileException
.
Basically, I have 5 .WAV files that I know are uncorrupted because they play perfectly fine when I open them in my music playing software. However, the Java program only works with 3 of them.
Oracle mentions support for this file format here. How can I make sure that all of my .WAV files are compatible with Java's audio API? Is there a foolproof way of playing .WAV files if for some reason they do not have the appropriate encoding?