Hi,
i'm wondering if it's posible to stream an internet Radio-Stream with Java?
I searched the inet and found a code for playing mp3 files on your pc.
The plugin is called JavaZoom.
import javax.sound.sampled.*;
import java.io.*;
public class Radio {
public static void main(String[] args) throws Exception{
}
public static void testPlay(String filename)
{
try {
File file = new File(filename);
AudioInputStream in= AudioSystem.getAudioInputStream(file);
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);
rawplay(decodedFormat, din);
in.close();
} catch (Exception e)
{
System.out.println(e.toString());
}
}
private static void rawplay(AudioFormat targetFormat, AudioInputStream din) throws IOException, LineUnavailableException
{
byte[] data = new byte[4096];
SourceDataLine line = getLine(targetFormat);
if (line != null)
{
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);
}
line.drain();
line.stop();
line.close();
din.close();
}
}
private static SourceDataLine getLine(AudioFormat audioFormat) throws LineUnavailableException
{
SourceDataLine res = null;
DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
res = (SourceDataLine) AudioSystem.getLine(info);
res.open(audioFormat);
return res;
}
}
This Code works. But now i want to to stream a mp3 file - but not just load it one time and then play it. I need i constant download. So i can stream a radio channel.
Anyone got the right code?
Thx