Reputation: 11
The following code works perfectly on 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();
But it's cause a javax.sound.sampled.LineUnavailableException
exception when started on 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)
Please, any ideas, what's wrong?
Upvotes: 1
Views: 883
Reputation: 14371
Your problem starts before your stack trace:
No protocol specified
xcb_connection_has_error() вернул true
Home directory not accessible: Отказано в доступе
No protocol specified
This is telling you that your home directory is not accessible, and that access it denied. This means either it does not exist, or you have a permissions problem. If your audio file is in your home directory, then your program cannot access it to play it.
File soundFile = new File("bell.wav");
This might be another problem (or part of the problem). bell.wav
is probably not in your working directory when you run your code... so if you have not modified your code to point to wherever you have this file located on your linux box, then the error's above make sense.
Before attempting to play the file, you should verify it exists on the filesystem and you have access to it.
Something like:
// 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
}
Upvotes: 1