domingo, 12 de mayo de 2013

Sound in Java

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

//...




Sonido en Java

See in English



Para colocar sonido a nuestra aplicación de escritorio en java, tenemos a nuestra disposición varias alternativas, sin embargo yo he hecho una clase sencilla para esa labor, la cual me ha servido para muchos proyectos.

Dicha clase es la siguiente:


  public class hiloSonido extends Thread{
        
           
            private Clip sonido;
            private boolean seguir;
            
        

         public hiloSonido(String rutaArchivo){
           
            seguir=true;
            try {
                sonido = AudioSystem.getClip();
                sonido.open(AudioSystem.getAudioInputStream(new File(rutaArchivo)));
                
            } catch (UnsupportedAudioFileException ex) {
              
                Logger.getLogger(ReproduceSonido.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
               
                Logger.getLogger(ReproduceSonido.class.getName()).log(Level.SEVERE, null, ex);
            } catch (LineUnavailableException ex) {
               
                Logger.getLogger(ReproduceSonido.class.getName()).log(Level.SEVERE, null, ex);
            }
         }
         
        @Override
         public void run(){
              
            sonido.start();
              
            // Espera mientras se esté reproduciendo.
            do{
                try {
                Thread.sleep(500);
                } catch (InterruptedException ex) {
                Logger.getLogger(ReproduceSonido.class.getName()).log(Level.SEVERE, null, ex);
                }
                
            }while (seguir && sonido.isActive());
            
            if(sonido.isActive()){
                sonido.stop();
            }
            
            // Se cierra el clip.
            sonido.close();
          
         
         }
        
        public void parar(){
            seguir=false;
        }
         
     }  



Para utilizar esta clase solo debemos hacer algo similar o igual a lo siguiente:


//...


// Ejemplo de ruta en Windows: C:\ruta\archivo_de_audio.wav
// Ejemplo de ruta en Linux: /home/usuario/archivo_de_audio.wav

hiloSonido obj = new hiloSonido("/home/usuario/archivo_de_audio.wav");
obj.start();

//...