Ver en Español
For placing sound to our java desktop Application, we have at our disposal multiple alternatives, however I have done a simple class for that endeavor, which has served me for many projects.
The Class is the next:
public class soundthread extends Thread{
private Clip sound;
private boolean continuex;
public soundthread(String filepath){
continuex=true;
try {
sound = AudioSystem.getClip();
sound.open(AudioSystem.getAudioInputStream(new File(filepath)));
} catch (UnsupportedAudioFileException ex) {
Logger.getLogger(soundthread.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(soundthread.class.getName()).log(Level.SEVERE, null, ex);
} catch (LineUnavailableException ex) {
Logger.getLogger(soundthread.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
public void run(){
sound.start();
// Wait while it is playing
do{
try {
Thread.sleep(500);
} catch (InterruptedException ex) {
Logger.getLogger(soundthread.class.getName()).log(Level.SEVERE, null, ex);
}
}while (continuex && sound.isActive());
if(sound.isActive()){
sound.stop();
}
// Close the clip.
sound.close();
}
public void stop(){
continuex=false;
}
}
For use this class, we should only do something similar or equal to the following:
//...
// Example of path in Windows: C:\path\audio_file.wav
// Example of path in Linux: /home/user/audio_file.wav
soundthread obj = new soundthread("/home/user/audio_file.wav");
obj.start();
//...
No hay comentarios:
Publicar un comentario