1
我發佈之前已經搜索論壇(我沒有找到我的問題的答案,從而使這篇文章)。 我想製作一個Java應用程序,播放來自shoutcast服務器的音頻流。我在網上看了一下,發現javazoom會對我想做的事情有用。所以我已經下載了他們的軟件包並開始實驗。 我想出這個,但我得到一個「javax.sound.sampled.UnsupportedAudioFileException:無法從輸入網址音頻輸入流」java + shoutcast流
下面是代碼:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package test;
/**
*
* @author George
*/
import java.io.*;
import java.util.Map;
import java.net.*;
import javax.sound.sampled.*;
public class play {
public void testPlay(String filename)
{
try {
// File file = new File(filename);
URL file= new URL(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();
din = AudioSystem.getAudioInputStream(decodedFormat, in);
// DecodedMpegAudioInputStream properties
if (din instanceof javazoom.spi.PropertiesContainer)
{
Map properties = ((javazoom.spi.PropertiesContainer) din).properties();
float[] equalizer = (float[]) properties.get("mp3.equalizer");
equalizer[0] = (float) 0.5;
equalizer[31] = (float) 0.25;
}
} catch (Exception e)
{
System.out.println(e);
}
}
private 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 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或其他 –