I have three buttons in my java program that can play three different sounds. Look at my code:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.*;
import java.awt.datatransfer.*;
public class VueEditeurEmail extends JFrame implements ActionListener {
private JPanel container = new JPanel();
private JPanel containerHaut = new JPanel();
private JPanel containerAction = new JPanel();
private JButton boutonEnvoyer = new JButton("Envoi");
private JButton boutonAnnuler = new JButton("Annuler");
private JButton contacts = new JButton("Contacts");
private JButton coller = new JButton("Coller");
private JTextArea textArea = new JTextArea();
private ChampsTexte champs1 = new ChampsTexte("Expéditeur :", "");
private ChampsTexte champs2 = new ChampsTexte("Destinataire :", "");
private ChampsTexte champs3 = new ChampsTexte("Objet :", "");
private JOptionPane jop1 = new JOptionPane();
private JOptionPane jop2 = new JOptionPane();
private JOptionPane jop3 = new JOptionPane();
final Clipboard clipboard = container.getToolkit().getSystemClipboard();
public VueEditeurEmail() {
this.setTitle("Expéditeur de Message");
this.setSize(500, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
containerHaut.setLayout(new BoxLayout(containerHaut, BoxLayout.Y_AXIS));
containerHaut.add(champs1);
containerHaut.add(champs2);
containerHaut.add(contacts);
containerHaut.add(coller);
containerHaut.add(champs3);
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
JScrollPane scrollPan = new JScrollPane(textArea);
container.setLayout(new BorderLayout());
container.setBackground(Color.white);
container.add(containerHaut, BorderLayout.NORTH);
container.add(scrollPan, BorderLayout.CENTER);
container.add(containerAction, BorderLayout.SOUTH);
containerAction.add(boutonEnvoyer, BorderLayout.EAST);
containerAction.add(boutonAnnuler, BorderLayout.WEST);
contacts.addActionListener(this);
boutonEnvoyer.addActionListener(this);
boutonAnnuler.addActionListener(this);
coller.addActionListener(this);
this.setContentPane(container);
this.setVisible(true);
containerHaut.setBackground(Color.GRAY);
champs1.setBackground(Color.GRAY);
champs2.setBackground(Color.GRAY);
champs3.setBackground(Color.GRAY);
textArea.setBackground(Color.YELLOW);
boutonEnvoyer.setBackground(Color.GRAY);
boutonAnnuler.setBackground(Color.GRAY);
}
public void playSound(String soundName){
try{
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File(soundName).getAbsoluteFile( ));
Clip clip = AudioSystem.getClip( );
clip.open(audioInputStream);
clip.start( );
}catch(Exception ex){
System.out.println("Erreur lors de la lecture du son");
ex.printStackTrace( );
}
}
public void actionPerformed(ActionEvent E) {
if(E.getSource() == boutonEnvoyer){
playSound("Envoyé.wav");
Email monEmail = new Email(champs1.getTexte(), champs2.getTexte(), champs3.getTexte(), textArea.getText());
monEmail.EnvoiSMTP("admpcsrv1.uha.fr", 25);
ImageIcon img1 = new ImageIcon("Images/succès.png");
jop1.showMessageDialog(null, "L'Email a bien été envoyé", "Envoi réussi", JOptionPane.INFORMATION_MESSAGE, img1);
System.exit(0);
}
else if(E.getSource() == boutonAnnuler){
playSound("Annulé.wav");
ImageIcon img2 = new ImageIcon("Images/annuler.png");
jop2.showMessageDialog(null, "Appuyer sur OK pour confirmer l'annulation", "Annulation", JOptionPane.INFORMATION_MESSAGE, img2);
System.exit(0);
}
else if (E.getSource() == contacts){
playSound("OuvertureContacts.wav");
ImageIcon img3 = new ImageIcon("Images/OuvertureContacts.png");
jop3.showMessageDialog(null, "Ouverture de la liste des contacts, appuyer sur Ok pour fermer ce popup", "Liste des contacts", JOptionPane.INFORMATION_MESSAGE, img3);
File file = new File("Fichiers/InfosContacts.xls");
try{
Desktop.getDesktop().open(file);
}catch(IOException e){
System.out.println("Ouverture du fichier impossible");
}
}
else if (E.getSource() == coller){
Transferable clipData = clipboard.getContents(clipboard);
try{
if(clipData.isDataFlavorSupported(DataFlavor.stringFlavor)){
String s = (String)(clipData.getTransferData(DataFlavor.stringFlavor));
champs2.fieldTexte.replaceSelection(s);
}
}catch(Exception ufe){}
}
}
public static void main(String[] args) {
VueEditeurEmail monEditeur = new VueEditeurEmail();
}
}
When I press the "contacts" button, the sound "OuvertureContacts.wav" is well played. But after, when I press the "boutonEnvoyer" button or the "boutonAnnuler" button, their sounds are not played ... What is the problem , I use the same code for the three buttons but when I press one button, the other doesn't play sounds after the first press !
I really appreciate your help, thanks in advance !