Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
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{
		
		//testPlay("C:...");
		
	}
	
	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);
	    // Play now.
	    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)
	  {
	    // 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();
	  }
	}

	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
Posted
Updated 10-Jul-11 5:19am
v2

1 solution

yeah I have one. Unfortunately it's stuck in some winamp-plugin. It will cost me one fortune of time to extract that - so you'd have to pay 2 fortunes for that!

My Lost Streaming MP3 Article[] is a good point to start at.

Winamp Plugin Developer WIKI[^] should bring some ideas on how to use these gives tools.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900