我想在代码中提供的URL处获取声音文件并播放它(mp3格式为).我查看了一些与此问题相关的Stack Overflow问题,他们都说过mp3plugin.jar这样做了.
在Eclipse中,我在配置构建路径下将其添加为外部jar(因为它位于我的Downloads文件夹中,不确定它是否是最佳位置).我再次运行它,它仍然给我这个错误:
javax.sound.sampled.UnsupportedAudioFileException:无法从 Starter.main
上的javax.sound.sampled.AudioSystem.getAudioInputStream(未知来源)的输入流中获取音频输入流
(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); …Run Code Online (Sandbox Code Playgroud)