问题描述
我试图在Ubuntu 20.04中从WAV文件中播放一些音频。我编写了代码以捕获LineUnavailableException,并尝试了每个混合器。即使对于所有选定的混音器来说mixer.isLinesupported()都是正确的,但每次我调用clip.open(ais)
时,都会抛出异常。我没有其他音频在播放,请关闭浏览器和除Eclipse之外的所有进程。
当我导出jar并在Windows上运行(我有双启动笔记本电脑)时,它第一次运行, 因此java.sound可以在Windows和我的硬件上运行。
在Linux上,几次输出是System.out.println(尝试每个混合器一个异常): “ LUE被抓到:线路尝试不可用”
最后,“找不到行”。
我的Java版本是: openjdk版本“ 1.8.0_265” OpenJDK运行时环境(内部版本1.8.0_265-8u265-b01-0ubuntu2〜20.04-b01) OpenJDK 64位服务器VM(内部版本25.265-b01,混合模式)
在Ubuntu 20.04上,java.sound是否真的有问题?我的硬件:
$ lspci 00:1f.3音频设备:英特尔公司Sunrise Point-LP HD音频(21版)
$ aplay -l PCH [HDA Intel PCH],设备0:ALC3227模拟[ALC3227模拟]
class AudioPlay2 implements LineListener
{
File soundFile;
jdialog playingDialog;
Clip clip;
public static void main(String[] args)
{
AudioPlay2 s = new AudioPlay2();
}
public AudioPlay2()
{
JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog(null);
soundFile = chooser.getSelectedFile();
System.out.println("Playing " + soundFile.getName());
Line.Info lineInfo = new Line.Info(Clip.class);
boolean foundLine = false;
for (mixer.Info mixerInfo : AudioSystem.getmixerInfo())
{
try
{
mixer mixer = AudioSystem.getmixer(mixerInfo);
if (mixer.isLinesupported(lineInfo))
{
Line line = AudioSystem.getLine(lineInfo);
clip = (Clip) line;
clip.addLineListener(this);
AudioInputStream ais = AudioSystem.getAudioInputStream(soundFile);
clip.open(ais);
foundLine = true;
break;
}
}
catch (LineUnavailableException lue)
{
System.out.println("LUE caught: Line tried was unavailable");
clip.removeLineListener(this);
}
catch(UnsupportedAudioFileException | IOException e)
{
System.out.println("Execption was " + e.getMessage());
clip.removeLineListener(this);
}
}
if (foundLine)
{
clip.start();
}
else
{
System.err.println("No Line Found");
}
}
public void update(LineEvent le) {
LineEvent.Type type = le.getType();
if (type == LineEvent.Type.OPEN) {
System.out.println("OPEN");
} else if (type == LineEvent.Type.CLOSE) {
System.out.println("CLOSE");
System.exit(0);
} else if (type == LineEvent.Type.START) {
System.out.println("START");
playingDialog.setVisible(true);
} else if (type == LineEvent.Type.STOP) {
System.out.println("STOP");
playingDialog.setVisible(false);
clip.close();
}
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)