Java声音在Windows中工作完美,在Linux中我们得到LineUnavailableException

Java声音在Windows中工作完美,在Linux中我们得到LineUnavailableException,java,linux,javasound,Java,Linux,Javasound,以下代码在Windows上运行良好: File soundFile = new File("bell.wav"); AudioInputStream ais = AudioSystem.getAudioInputStream(soundFile); Clip clip = AudioSystem.getClip(); clip.open(ais); clip.setFramePosition(0); clip.start(); Thread.sleep(clip.getMicrosecondL

以下代码在Windows上运行良好:

File soundFile = new File("bell.wav");
AudioInputStream ais = AudioSystem.getAudioInputStream(soundFile);
Clip clip = AudioSystem.getClip();
clip.open(ais);
clip.setFramePosition(0);
clip.start(); 
Thread.sleep(clip.getMicrosecondLength()/1000);
clip.stop();
clip.close();
但这会导致在Linux上启动时出现
javax.sound.sampled.LineUnavailableException
异常:

No protocol specified
xcb_connection_has_error() вернул true
Home directory not accessible: Отказано в доступе
No protocol specified
javax.sound.sampled.LineUnavailableException
    at org.classpath.icedtea.pulseaudio.PulseAudioMixer.openImpl(PulseAudioMixer.java:714)
    at org.classpath.icedtea.pulseaudio.PulseAudioMixer.openLocal(PulseAudioMixer.java:588)
    at org.classpath.icedtea.pulseaudio.PulseAudioMixer.openLocal(PulseAudioMixer.java:584)
    at org.classpath.icedtea.pulseaudio.PulseAudioMixer.open(PulseAudioMixer.java:579)
    at org.classpath.icedtea.pulseaudio.PulseAudioDataLine.open(PulseAudioDataLine.java:94)
    at org.classpath.icedtea.pulseaudio.PulseAudioDataLine.open(PulseAudioDataLine.java:283)
    at org.classpath.icedtea.pulseaudio.PulseAudioClip.open(PulseAudioClip.java:402)
    at org.classpath.icedtea.pulseaudio.PulseAudioClip.open(PulseAudioClip.java:453)
    at beans.SoundDriver.PlayText(SoundDriver.java:41)  

有什么想法吗?

在堆栈跟踪之前,问题就开始了:

No protocol specified
xcb_connection_has_error() вернул true
Home directory not accessible: Отказано в доступе
No protocol specified
这表示您的主目录不可访问,访问被拒绝。这意味着它不存在,或者您有权限问题。如果您的音频文件位于主目录中,则您的程序无法访问它来播放它

File soundFile = new File("bell.wav");
这可能是另一个问题(或部分问题)<运行代码时,code>bell.wav可能不在您的工作目录中。。。因此,如果您没有将代码修改为指向linux机器上该文件所在的位置,那么上面的错误是有意义的

在尝试播放文件之前,您应该验证文件系统中是否存在该文件,以及您是否有权访问该文件

比如:

// if all your sound files are in the same directory
// you can make this final and set it in your sound
// player's constructor...
private final File soundDir;

public MySoundPlayer(final File soundDir) {
    this.soundDir = soundDir;
}

// ... 

public void playSound(final String soundFileName) {
    File soundFile = new File(soundDir, soundFileName);
    if (!soundFind.exists()) {
        // do something here, maybe throw exception...
        // or return out of your function early...
        throw new IllegalArgumentException(
            "Cannot access sound file: " + soundFileName);
    }
    // if you made it to here, now play your file
}

在Windows中,此应用程序从哪个目录运行?另外,在Linux环境中,
bell.wav
在哪里?我只知道,
bell.wav
是Windows系统的声音(如果内存没有问题的话)。不。wav是一种标准音频格式,而不是windows特定格式。您是否检查了进程运行java程序的权限?尝试使用管理员权限运行它。@Steffen,我知道
wav
文件是标准的未压缩音频格式。我特别询问的是bell.wav
以及它是否存在于OP的Linux环境中。我认为OP的代码包含特定于Windows的配置。此外,应用程序可能没有访问该目录的权限。