java输入错误重新输入无法获取_java – 无法从输入流中获取音频输入流

我想在代码中提供的URL处获取声音文件并播放它(它是mp3格式).我查看了一些与此问题相关的Stack Overflow问题,他们都说得到mp3plugin.jar所以我做了.

在Eclipse中,我在配置构建路径下将其添加为外部jar(因为它位于我的Downloads文件夹中,不确定它是否是最佳位置).我再次运行它,它仍然给我这个错误:

javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input stream

at javax.sound.sampled.AudioSystem.getAudioInputStream(Unknown Source)

at Starter.main(Starter.java:21)

这是代码:

public class Starter {

public static void main(String[] args) {

AudioInputStream din = null;

try {

URL url = new URL("http://c5.rbxcdn.com/2e6d33a5b3b1d8f250c395816ff9f145");

HttpURLConnection httpcon = (HttpURLConnection) url.openConnection();

InputStream bufferedIn = new BufferedInputStream(httpcon.getInputStream());

AudioInputStream in = AudioSystem.getAudioInputStream(bufferedIn);

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

DataLine.Info info = new DataLine.Info(SourceDataLine.class, decodedFormat);

SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info);

if(line != null) {

line.open(decodedFormat);

byte[] data = new byte[4096];

// Start

line.start();

int nBytesRead;

while ((nBytesRead = din.read(data, 0, data.length)) != -1) {

line.write(data, 0, nBytesRead);

}

// Stop

line.drain();

line.stop();

line.close();

din.close();

}

}

catch(Exception e) {

e.printStackTrace();

}

finally {

if(din != null) {

try { din.close(); } catch(IOException e) { }

}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值