How do I get a button to eject me from the program but before that play a wav sound? (Jframe Java Netbeans)

0

I want a button to exit my running program, but in the same way I want to press a sound first (before launching the program) ... How should I achieve it?

    
asked by Asuna Verdejo 14.04.2017 в 11:34
source

2 answers

0

Good morning

First apologies, my answer is from memory and may not be as accurate as I would like.

1: It depends on which button you want the program to close, for example, you can simply overwrite the behavior of the main window so that when it finishes it ends the execution and, in the process, performs the procedures that you want. A typical example is usually to define this logic in the main JFrame constructor, such that:

public Aplicacion() {

    this.setResizable(false);
    // Definimos comportamiento en el cierre
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.addWindowListener(new WindowAdapter() {
        @Override
        public void windowClosing(WindowEvent e) {
            //bd.eliminarTablas();
            bd.cerrarDB();
        }
    });              

    initComponents(); // Inciamos el resto de componentes
}

Regarding the sound, I think that once you can play it, it has an "isRunning", "isPlaying" or similar. You could do something similar to

while(sonido.isRunning)
{
 thread.sleep(1000);
}

Being, in the example that I have put something similar to (sorry, here I put some pseudocode, but it should not cost you to translate it)

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.addWindowListener(new WindowAdapter() {
    @Override
    public void windowClosing(WindowEvent e) {
        sonido.start();
        while (sonido.isRunning)
        {
          thread.sleep(1000); // No es necesario realmente, pero es más limpio que tener el bucle constantemente corriendo
        }
        sonido.close();
      // Cualquier otra cosa o metodo que quieras ejecutar antes del cierre
    }
});

Greetings

    
answered by 14.04.2017 в 12:28
0

To play a sound you can use AudioSystem creating a Clip from a .wav file:

import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;

/**
 * @author snolde
 *
 */
public class Audio implements Runnable, ActionListener{

    private long sleep;
    private File wav;



    public Audio(File wav, long sleep){
        this.wav = wav;
        this.sleep = sleep; // tiempo de espera
    }

    @Override
    actionPerformed(ActionEvent e){
        run();
        System.exit(0);            
    }

    @Override
    public void run() {
        AudioInputStream ais;
        try {
            ais = AudioSystem.getAudioInputStream(wav);
            try {
                Clip clip = AudioSystem.getClip();
                clip.open(ais);
                try {
                    clip.start();
                    try {
                        Thread.sleep(sleep);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    clip.drain();
                } finally {
                    clip.close();
                }
            } catch (LineUnavailableException e) {
                e.printStackTrace();
            } finally {
                ais.close();
            }
        } catch (UnsupportedAudioFileException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new Audio(new File("Bell.wav"),100L).run();
    }
}

The waiting time is needed to not close the clip before it touches. The full duration is not needed. I implemented it as Runnable, así alternativamente se puede usar para tocar sonidos en un Thread ':

new Audio(new File("ejemplo.wav"),20L).start();
// seguir con código mientras el sonido suena 

By implementing ActionListener , the sound can simply be added to JButton :

JButton button = new JButton("Cerrar");
button.addActionListener(new Audio(new File("exit.wav"), 20L));
    
answered by 15.04.2017 в 06:20