本文介绍了JavaZoom-广播流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想知道是否可以使用Java播放Internet Radio-Stream?

我搜索了inet,找到了在您的PC上播放mp3文件的代码.
该插件称为JavaZoom.

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;
	} 
	
}



此代码有效.但是现在我想流式传输mp3文件-而不是只加载一次然后播放它.我需要不断下载.这样我就可以播放广播频道.

任何人都有正确的密码吗?

Thx



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

推荐答案


这篇关于JavaZoom-广播流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 06:25