java播放音频流,Java的音频流(mp3spi LIB),UnsupportedAudioFileException

在尝试使用Java的MP3SPI库播放Icecast MP3流时,遇到UnsupportedAudioFileException。问题在于MP3SPI不支持m3u播放列表文件。解决方案是直接使用m3u文件中引用的真实MP3文件或流URL。检查代码显示,库不识别m3u文件格式,而是在寻找如WAV、AU、AIFF等其他音频格式。
摘要由CSDN通过智能技术生成

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 t

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值