I have seen multiple Stack Overflow questions regarding streaming MP3 streams (like Icecast). They all say to use the MP3SPI libraries, which I am. MP3SPI is for allowing support to audio/mpeg mime types. That's what my Icecast stream is. I have all three jar files in my classpath correctly, but while using the same code they provide in an example, I still get UnsupportedAudioFileException:
javax.sound.sampled.UnsupportedAudioFileException: could not get audio input str
eam from input URL
at javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:
1153)
at DJUtils.testPlay(DJUtils.java:16)
at DJ.play(DJ.java:13)
at DJ.init(DJ.java:4)
at Loader.main(Loader.java:69)
Here's my code:
public static void testPlay(){
try {
AudioInputStream in= AudioSystem.getAudioInputStream(new URL("http://localhost:8000/listen.m3u"));
AudioInputStream din = null;
AudioFormat baseFormat = in.getFormat();
AudioFormat decodedFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
baseFormat.getSampleRate(),
16,
baseFormat.getChannels(),
baseFormat.getChannels() * 2,
baseFormat.getSampleRate(),
false);
din = AudioSystem.getAudioInputStream(decodedFormat, in);
// Play now.
rawplay(decodedFormat, din);
in.close();
} catch (Exception e){
e.printStackTrace();
}
}
private static void rawplay(AudioFormat targetFormat, AudioInputStream din) throws LineUnavailableException, IOException{
try{
byte[] data = new byte[4096];
SourceDataLine line = getLine(targetFormat);
if (line != null)
{
// Start
line.start();
int nBytesRead = 0, nBytesWritten = 0;
while (nBytesRead != -1)
{
nBytesRead = din.read(data, 0, data.length);
if (nBytesRead != -1) nBytesWritten = line.write(data, 0, nBytesRead);
}
// Stop
line.drain();
line.stop();
line.close();
din.close();
}
}catch(IOException e){
e.printStackTrace();
}
}
private static SourceDataLine getLine(AudioFormat audioFormat) throws LineUnavailableException{
try{
SourceDataLine res = null;
DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
res = (SourceDataLine) AudioSystem.getLine(info);
res.open(audioFormat);
return res;
}catch(LineUnavailableException e){
e.printStackTrace();
return null;
}
}
My start script for t