使用java播放mp3的LineUnavailableException [英] LineUnavailableException for playing mp3 with java
本文介绍了使用java播放mp3的LineUnavailableException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的目标是从Java播放mp3文件。对于我采用的每种方法,它总是以 LineUnavailableException
失败。
My goal is to play an mp3 file from Java. With every approach that I took, it always fails with a LineUnavailableException
.
AudioInputStream inputStream = AudioSystem.getAudioInputStream(new URL("http://localhost:8080/agriserver/facebook/sound/test6.mp3"));
Clip clip = AudioSystem.getClip(info);
clip.open(inputStream);
clip.start();
尝试修复失败:
- 使用Sun的mp3插件。
- 使用Jlayer第三方库
- 使用Tritonus第三方库
- 使用Sony Sound Forge,Adobe Sound Booth重新编码mp3,一切顺利
- 使用不同的编码率和采样率重新编码mp3
- 尝试使用JMF
- 使用来自互联网的随机mp3在其他应用程序中正常播放
- 阅读具有相同错误的帖子。所有帖子都没有帮助解决问题的答案。
- Use Sun's mp3 plugin.
- Use Jlayer 3rd party library
- Use Tritonus 3rd party library
- Re-encode the mp3 with Sony Sound Forge, Adobe Sound Booth, all no luck
- Re-encode the mp3 with different encode rates and sampling rates
- Try to use JMF
- Use random mp3 from the Internet that plays fine in other applications
- Read postings with the same error. None of the postings have an answer that helped resolve the issue.
以下是例外情况:
Exception in thread "main" javax.sound.sampled.LineUnavailableException: line with format MPEG1L3 48000.0 Hz, unknown bits per sample, stereo, unknown frame size, 41.666668 frames/second, not supported.
at com.sun.media.sound.DirectAudioDevice$DirectDL.implOpen(DirectAudioDevice.java:494)
at com.sun.media.sound.DirectAudioDevice$DirectClip.implOpen(DirectAudioDevice.java:1280)
at com.sun.media.sound.AbstractDataLine.open(AbstractDataLine.java:107)
at com.sun.media.sound.DirectAudioDevice$DirectClip.open(DirectAudioDevice.java:1061)
at com.sun.media.sound.DirectAudioDevice$DirectClip.open(DirectAudioDevice.java:1151)
at Demo.playMp3(Demo.java:83)
推荐答案
显然,必须将mp3读入一个流。必须将该流读入第二个流以对其进行解码。以下代码有效:
Apparently, the mp3 has to be read into one stream. That stream has to be read into a second stream to decode it. The below code worked:
// read the file
AudioInputStream rawInput = AudioSystem.getAudioInputStream(new ByteArrayInputStream(data));
// decode mp3
AudioFormat baseFormat = rawInput.getFormat();
AudioFormat decodedFormat = new AudioFormat(
AudioFormat.Encoding.PCM_SIGNED, // Encoding to use
baseFormat.getSampleRate(), // sample rate (same as base format)
16, // sample size in bits (thx to Javazoom)
baseFormat.getChannels(), // # of Channels
baseFormat.getChannels()*2, // Frame Size
baseFormat.getSampleRate(), // Frame Rate
false // Big Endian
);
AudioInputStream decodedInput = AudioSystem.getAudioInputStream(decodedFormat, rawInput);
这篇关于使用java播放mp3的LineUnavailableException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文