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 this project:
java -Dfile.encoding=Cp1252 -classpath bin;lib/libs.jar;lib/graphics.jar;lib/mp3spi/mp3spi.jar;lib/mp3spi/jl.jar;lib/mp3spi/tritonus.jar; Loader
And my Icecast control panel says it's currently streaming audio/mpeg
. I can access my stream fine by opening that URL in my code up in any media player. Can someone point out what I'm doing wrong? Thanks!
mp3spi library as such does not consider m3u playlist file as a supported file.
Try using the real stream url used inside the m3u file. ie the url directly to the mp3 file or stream.
Check the function below. Its straight out of MpegAudioFileReader.java, mp3spi library uses to identify the format of the data stream you presented using URL. It doesn't recognize an m3u file. You can check the source if you want from http://www.javazoom.net/mp3spi/sources.html.