无法从输入流中获取音频输入流
Could not get audio input stream from input stream
我想在代码提供的URL处获取声音文件并播放(它是mp3
格式)。我查看了一些与此问题相关的 Stack Overflow 问题,他们都说得到 mp3plugin.jar
所以我做到了。
在 Eclipse 中,我将其添加为外部 jar(因为它位于我的下载文件夹内,不确定这是否是放置它的最佳位置)在配置构建路径下。我再次 运行 它仍然给我这个错误:
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) { }
}
}
}
}
你需要得到http://www.javazoom.net/mp3spi/docs/doc1.9.4/javazoom/spi/mpeg/sampled/file/MpegAudioFileReader.html
下载 jar。我的类路径为
.;C:\Vision\Audio\libs\vorbisspi1.0.3.jar;C:\Vision\Audio\libs\tritonus_share.jar;C:\Vision\Audio\libs\tritonus_remaining-0.3.6.jar;C:\Vision\Audio\libs\jorbis-0.0.15.jar;C:\Vision\Audio\libs\jogg-0.0.7.jar;C:\Vision\Audio\libs\jl1.0.jar;C:\Vision\Audio\libs\mp3spi1.9.4.jar;
您可能只需要 mp3spi1.9。4.jar - 其中一些用于其他格式,但我不确定,所以我将它们全部包括在内。
则有如下程序
public AudioInputStream readMP3URL(String f) {
AudioInputStream audioInputStream = null;
AudioFormat targetFormat = null;
try {
AudioInputStream in = null;
MpegAudioFileReader mp = new MpegAudioFileReader();
in = mp.getAudioInputStream(new URL(f));
AudioFormat baseFormat = in.getFormat();
targetFormat = new AudioFormat(
AudioFormat.Encoding.PCM_SIGNED,
baseFormat.getSampleRate(),
16,
baseFormat.getChannels(),
baseFormat.getChannels() * 2,
baseFormat.getSampleRate(),
false);
audioInputStream = AudioSystem.getAudioInputStream(targetFormat, in);
}
catch (Exception ue) {
System.out.println("\nUnsupported Audio");
}
return audioInputStream;
}
public void readURL() {
int i, j, k = 0, l, basicU = 1024;
AudioFormat targetFormat = null;
audioInputStream = readMP3URL("http://c5.rbxcdn.com/2e6d33a5b3b1d8f250c395816ff9f145");
if (audioInputStream == null) System.out.println("null audiostream");
targetFormat = audioInputStream.getFormat();
byte[] data = new byte[basicU];
DataLine.Info dinfo = new DataLine.Info(SourceDataLine.class, targetFormat);
SourceDataLine line = null;
try {
line = (SourceDataLine) AudioSystem.getLine(dinfo);
if (line != null) {
line.open(targetFormat);
line.start();
while ((k = audioInputStream.read(data, 0, data.length)) != -1) {
line.write(data, 0, k);
}
line.stop();
line.close();
}
}
catch (Exception ex) {
ex.printStackTrace();
System.out.println("audio problem " + ex);
}
}
我想在代码提供的URL处获取声音文件并播放(它是mp3
格式)。我查看了一些与此问题相关的 Stack Overflow 问题,他们都说得到 mp3plugin.jar
所以我做到了。
在 Eclipse 中,我将其添加为外部 jar(因为它位于我的下载文件夹内,不确定这是否是放置它的最佳位置)在配置构建路径下。我再次 运行 它仍然给我这个错误:
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) { }
}
}
}
}
你需要得到http://www.javazoom.net/mp3spi/docs/doc1.9.4/javazoom/spi/mpeg/sampled/file/MpegAudioFileReader.html
下载 jar。我的类路径为
.;C:\Vision\Audio\libs\vorbisspi1.0.3.jar;C:\Vision\Audio\libs\tritonus_share.jar;C:\Vision\Audio\libs\tritonus_remaining-0.3.6.jar;C:\Vision\Audio\libs\jorbis-0.0.15.jar;C:\Vision\Audio\libs\jogg-0.0.7.jar;C:\Vision\Audio\libs\jl1.0.jar;C:\Vision\Audio\libs\mp3spi1.9.4.jar;
您可能只需要 mp3spi1.9。4.jar - 其中一些用于其他格式,但我不确定,所以我将它们全部包括在内。
则有如下程序
public AudioInputStream readMP3URL(String f) {
AudioInputStream audioInputStream = null;
AudioFormat targetFormat = null;
try {
AudioInputStream in = null;
MpegAudioFileReader mp = new MpegAudioFileReader();
in = mp.getAudioInputStream(new URL(f));
AudioFormat baseFormat = in.getFormat();
targetFormat = new AudioFormat(
AudioFormat.Encoding.PCM_SIGNED,
baseFormat.getSampleRate(),
16,
baseFormat.getChannels(),
baseFormat.getChannels() * 2,
baseFormat.getSampleRate(),
false);
audioInputStream = AudioSystem.getAudioInputStream(targetFormat, in);
}
catch (Exception ue) {
System.out.println("\nUnsupported Audio");
}
return audioInputStream;
}
public void readURL() {
int i, j, k = 0, l, basicU = 1024;
AudioFormat targetFormat = null;
audioInputStream = readMP3URL("http://c5.rbxcdn.com/2e6d33a5b3b1d8f250c395816ff9f145");
if (audioInputStream == null) System.out.println("null audiostream");
targetFormat = audioInputStream.getFormat();
byte[] data = new byte[basicU];
DataLine.Info dinfo = new DataLine.Info(SourceDataLine.class, targetFormat);
SourceDataLine line = null;
try {
line = (SourceDataLine) AudioSystem.getLine(dinfo);
if (line != null) {
line.open(targetFormat);
line.start();
while ((k = audioInputStream.read(data, 0, data.length)) != -1) {
line.write(data, 0, k);
}
line.stop();
line.close();
}
}
catch (Exception ex) {
ex.printStackTrace();
System.out.println("audio problem " + ex);
}
}