2015-02-05 16 views
1

下面的代码工作完全在Windows上:Java的声音可以完美运行在Windows,Linux中我们得到了LineUnavailableException

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(); 

但它是导致javax.sound.sampled.LineUnavailableException例外,在Linux启动时:

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) 

请,任何想法,有什么不对?

+0

在Windows中,在哪个目录是运行此应用程序?另外,你的Linux环境中的'bell.wav'在哪里?我所知道的是'bell.wav'是一个Windows系统声音(如果内存正确地为我服务)。 – 2015-02-05 15:32:42

+0

nope。 wav是一种标准音频格式,而不是Windows特定的 – Steffen 2015-02-05 15:38:58

+0

您是否检查过运行java程序的权限?尝试使用管理员权限运行它。 – sphinks 2015-02-05 15:54:25

回答

1

你的问题,你的堆栈跟踪开始之前:

No protocol specified 
xcb_connection_has_error() вернул true 
Home directory not accessible: Отказано в доступе 
No protocol specified 

这是告诉你,你的home目录无法访问,并访问其拒绝。这意味着它不存在,或者您有权限问题。如果您的音频文件位于您的主目录中,则您的程序无法访问它来播放它。

File soundFile = new File("bell.wav"); 

这可能是另一个问题(或问题的一部分)。当你运行你的代码时,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 
} 
相关问题