2015-02-05 19 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 
} 
相關問題