eclipse 播放声音java错误(无线匹配接口Clip支持格式)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5808560/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
error playing sound java (No line matching interface Clip supporting format)
提问by pveeckhout
We are trying to integrated sound in one of our project, my team members don't get this error, and I get it on two different machines.
我们正在尝试在我们的一个项目中集成声音,我的团队成员没有收到此错误,并且我在两台不同的机器上收到了它。
Stack trace:
堆栈跟踪:
Exception in thread "SoundPlayer" java.lang.IllegalArgumentException: No line matching interface Clip supporting format PCM_SIGNED 16000.0 Hz, 16 bit, stereo, 4 bytes/frame, little-endian, and buffers of 11129272 to 11129272 bytes is supported.
at javax.sound.sampled.AudioSystem.getLine(Unknown Source)
at sound.Music.run(Music.java:86)
at java.lang.Thread.run(Unknown Source)
Code:
代码:
package sound;
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.FloatControl;
import javax.sound.sampled.LineEvent;
import javax.sound.sampled.LineListener;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
public class Music implements LineListener, Runnable
{
private File soundFile;
private Thread thread;
private static Music player;
private Music audio;
private Clip clip;
public Music()
{
}
public void playSiren(String musicFileName)
{
Music p = getPlayer();
p.playSirenFile(musicFileName);
}
private void playSirenFile(String musicFileName)
{
this.soundFile = new File("Music/"+musicFileName+".wav");
thread = new Thread(this);
thread.setName("SoundPlayer");
thread.start();
}
public void run()
{
try
{
AudioInputStream stream = AudioSystem.getAudioInputStream(this.soundFile);
AudioFormat format = stream.getFormat();
/**
* we can't yet open the device for ALAW/ULAW playback, convert
* ALAW/ULAW to PCM
*/
if ((format.getEncoding() == AudioFormat.Encoding.ULAW) || (format.getEncoding() == AudioFormat.Encoding.ALAW))
{
AudioFormat tmp = new AudioFormat(
AudioFormat.Encoding.PCM_SIGNED,
format.getSampleRate(),
format.getSampleSizeInBits() * 2, format.getChannels(),
format.getFrameSize() * 2, format.getFrameRate(), true);
stream = AudioSystem.getAudioInputStream(tmp, stream);
format = tmp;
}
DataLine.Info info = new DataLine.Info(Clip.class, stream
.getFormat(), ((int) stream.getFrameLength() * format
.getFrameSize()));
clip = (Clip) AudioSystem.getLine(info);
clip.addLineListener(this);
clip.open(stream);
clip.start();
try
{
thread.sleep(99);
}
catch (Exception e)
{
}
while (clip.isActive() && thread != null)
{
try
{
thread.sleep(99);
}
catch (Exception e)
{
break;
}
}
clip.loop(99999999);
}
catch (UnsupportedAudioFileException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (LineUnavailableException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static Music getPlayer()
{
if (player == null)
{
player = new Music();
}
return player;
}
public void update(LineEvent event)
{
}
public void stopClip()
{
clip.stop();
}
public void closeClip()
{
clip.close();
}
public void startClip()
{
clip.start();
}
public void volume(float volume)
{
/*
FloatControl gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
gainControl.setValue(-50.0f); // Reduce volume IN DECIBELS
clip.start();
*/
}
}
We call this class from domainController with
我们从 domainController 调用这个类
audio = new Music();
audio.playSiren("stillAliveDecent");
does Anyone have an idea how this exception can be resolved? I tried reinstalling my editor software (Eclipse) but to no avail.
有没有人知道如何解决这个异常?我尝试重新安装我的编辑器软件 (Eclipse) 但无济于事。
thanks allot in advance.
感谢提前分配。
Edit
编辑
We just tried switching the sound file. We tried running it with much smaller file. this now works, but once we switch back to the larger .wav file (10+MB) I get the exception again.
我们只是尝试切换声音文件。我们尝试用更小的文件运行它。这现在可以工作了,但是一旦我们切换回更大的 .wav 文件(10+MB),我就会再次遇到异常。
Only using smaller files is not really an option as we would like to use some self made songs which are quite long.
只使用较小的文件并不是一个真正的选择,因为我们想使用一些很长的自制歌曲。
Edit 2
I'm quite sure it isn't a corrupted wav. we recompiled it, even used another wave of similar length and size, and i'm still the only one getting this error.
编辑 2
我很确定它不是损坏的 wav。我们重新编译了它,甚至使用了另一波长度和大小相似的波浪,但我仍然是唯一一个遇到此错误的人。
some extra requested info:
一些额外要求的信息:
OS: Windows 7 64bit Ultimate
JDK: 1.6.0_22
操作系统:Windows 7 64 位旗舰版
JDK:1.6.0_22
Edit 3
编辑 3
After some wave creating and playing we have come to the conclusion that for some reason I can't play wave's larger than 2MB.
经过一些 wave 的创建和播放,我们得出的结论是,由于某种原因,我无法播放大于 2MB 的 wave。
Still why aren't my teammates affected by this?
还有为什么我的队友不受此影响?
采纳答案by Jonathan
You can actually play sound above 40 mb, if needed, thats how far i went :p, the problem is mostly eclipse, and to be more exact its the .metadata folder in your workspace i think its like a small plugin that only gets uploaded half of the time, so the problem lies with your editor and not the code, the code above is working perfectly since i could play songs without any trouble. Make sure your paths are correct, and try to get a correct version of the .metadata and you should be fine. A friend of mine had the same problem, and i gave him my copy of the workspace and .metadata and it worked perfectly.
如果需要,您实际上可以播放 40 mb 以上的声音,这就是我走了多远:p,问题主要是 eclipse,更准确地说,它是您工作区中的 .metadata 文件夹,我认为它就像一个只能上传的小插件一半的时间,所以问题在于您的编辑器而不是代码,上面的代码运行良好,因为我可以毫无困难地播放歌曲。确保您的路径正确,并尝试获得正确版本的 .metadata,您应该没问题。我的一个朋友遇到了同样的问题,我给了他我的工作区和 .metadata 副本,它运行良好。
回答by Gordolio
I was experiencing this same problem on a raspberry pi. It would play the first 5 files just fine, then I'd get the error. It turned out that I was not closing the clip when I needed to.
我在树莓派上遇到了同样的问题。它会很好地播放前 5 个文件,然后我会收到错误消息。事实证明,我没有在需要时关闭剪辑。
Clip clip = AudioSystem.getClip();
clip.addLineListener(event -> {
if(LineEvent.Type.STOP.equals(event.getType())) {
clip.close();
}
});
ByteArrayInputStream audioBytes = new ByteArrayInputStream(SOUNDS.get(file));
AudioInputStream inputStream = AudioSystem.getAudioInputStream(audioBytes);
clip.open(inputStream);
clip.start();
After adding the line listener and closing the clip when it stopped, the errors went away.
添加线路侦听器并在剪辑停止时关闭剪辑后,错误消失了。