IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

AWT/Swing Java Discussion :

Jouer un son wav dans un Event


Sujet :

AWT/Swing Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Profil pro
    Inscrit en
    Juin 2008
    Messages
    77
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2008
    Messages : 77
    Par défaut Jouer un son wav dans un Event
    Bonjour à tous,

    Je travaille sous Netbeans, sur une borne faite en IHM. Je voudrais jouer un son wav lorsque je passe le curseur de la souris sur un panel appelé cellule.

    Je fais donc :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    private void celluleMouseEntered(java.awt.event.MouseEvent evt) {                                     
        //jouer le son
    }
    Pour jouer un son, je dispose d'une classe, WavPlayer, avec une méthode run() qui permet de lire un son.

    Ma borne et ma classe WavPlayer sont rangées dans le même package, je voudrais donc utiliser ma méthode run() pour jouer le son dans ma borne, en faisant

    Mais Netbeans me genère une erreur me disant qu'il ne trouve pas cette méthode.

    Si quelqu'un peut m'aider à corriger cette erreur, merci d'avance

  2. #2
    Membre Expert
    Avatar de supersnail
    Homme Profil pro
    Inscrit en
    Novembre 2006
    Messages
    1 719
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Novembre 2006
    Messages : 1 719
    Par défaut
    fais voir le code complet...

  3. #3
    Membre confirmé
    Profil pro
    Inscrit en
    Juin 2008
    Messages
    77
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2008
    Messages : 77
    Par défaut
    Le source de mon IHM :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
     
    package Borne;
    import java.awt.Color;
     
    public class BorneMAE extends javax.swing.JFrame {
     
        /** Creates new form BorneMAE */
        public BorneMAE() {
            initComponents();
        }
     
        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
     
            ledVerte = new javax.swing.JPanel();
            ledRouge = new javax.swing.JPanel();
            jPanel3 = new javax.swing.JPanel();
            jPanel4 = new javax.swing.JPanel();
            lcdLigne1 = new javax.swing.JLabel();
            lcdLigne2 = new javax.swing.JLabel();
            jPanel5 = new javax.swing.JPanel();
            codeCarte = new javax.swing.JComboBox();
            cellule = new javax.swing.JPanel();
     
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
     
            ledVerte.setBackground(new java.awt.Color(0, 102, 0));
            ledVerte.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
     
            org.jdesktop.layout.GroupLayout ledVerteLayout = new org.jdesktop.layout.GroupLayout(ledVerte);
            ledVerte.setLayout(ledVerteLayout);
            ledVerteLayout.setHorizontalGroup(
                ledVerteLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(0, 30, Short.MAX_VALUE)
            );
            ledVerteLayout.setVerticalGroup(
                ledVerteLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(0, 29, Short.MAX_VALUE)
            );
     
            ledRouge.setBackground(new java.awt.Color(204, 0, 0));
            ledRouge.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
     
            org.jdesktop.layout.GroupLayout ledRougeLayout = new org.jdesktop.layout.GroupLayout(ledRouge);
            ledRouge.setLayout(ledRougeLayout);
            ledRougeLayout.setHorizontalGroup(
                ledRougeLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(0, 29, Short.MAX_VALUE)
            );
            ledRougeLayout.setVerticalGroup(
                ledRougeLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(0, 29, Short.MAX_VALUE)
            );
     
            jPanel3.setBackground(new java.awt.Color(0, 0, 0));
     
            jPanel4.setBackground(new java.awt.Color(0, 153, 153));
     
            lcdLigne1.setText("............Bonjour............");
     
            lcdLigne2.setText("     Introduire une carte");
     
            org.jdesktop.layout.GroupLayout jPanel4Layout = new org.jdesktop.layout.GroupLayout(jPanel4);
            jPanel4.setLayout(jPanel4Layout);
            jPanel4Layout.setHorizontalGroup(
                jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel4Layout.createSequentialGroup()
                    .addContainerGap()
                    .add(jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                        .add(org.jdesktop.layout.GroupLayout.LEADING, lcdLigne2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 133, Short.MAX_VALUE)
                        .add(org.jdesktop.layout.GroupLayout.LEADING, lcdLigne1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addContainerGap())
            );
            jPanel4Layout.setVerticalGroup(
                jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(jPanel4Layout.createSequentialGroup()
                    .addContainerGap()
                    .add(lcdLigne1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 21, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(18, 18, 18)
                    .add(lcdLigne2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 21, Short.MAX_VALUE)
                    .addContainerGap())
            );
     
            org.jdesktop.layout.GroupLayout jPanel3Layout = new org.jdesktop.layout.GroupLayout(jPanel3);
            jPanel3.setLayout(jPanel3Layout);
            jPanel3Layout.setHorizontalGroup(
                jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(jPanel3Layout.createSequentialGroup()
                    .addContainerGap()
                    .add(jPanel4, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addContainerGap())
            );
            jPanel3Layout.setVerticalGroup(
                jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel3Layout.createSequentialGroup()
                    .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(jPanel4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap())
            );
     
            jPanel5.setBackground(new java.awt.Color(102, 102, 102));
     
            codeCarte.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "0312093-00101", "0312093-00121", "0312093-00564", "0312093-02114", "0312093-01524", "0312093-01530", "0312093-02115", "0312093-02116", "0312093-02118", "0312093-02123", "0312093-02124" }));
            codeCarte.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    codeCarteActionPerformed(evt);
                }
            });
     
            org.jdesktop.layout.GroupLayout jPanel5Layout = new org.jdesktop.layout.GroupLayout(jPanel5);
            jPanel5.setLayout(jPanel5Layout);
            jPanel5Layout.setHorizontalGroup(
                jPanel5Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(jPanel5Layout.createSequentialGroup()
                    .addContainerGap()
                    .add(codeCarte, 0, 117, Short.MAX_VALUE)
                    .addContainerGap())
            );
            jPanel5Layout.setVerticalGroup(
                jPanel5Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel5Layout.createSequentialGroup()
                    .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(codeCarte, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap())
            );
     
            cellule.setBackground(new java.awt.Color(255, 255, 0));
            cellule.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
            cellule.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseEntered(java.awt.event.MouseEvent evt) {
                    celluleMouseEntered(evt);
                }
            });
     
            org.jdesktop.layout.GroupLayout celluleLayout = new org.jdesktop.layout.GroupLayout(cellule);
            cellule.setLayout(celluleLayout);
            celluleLayout.setHorizontalGroup(
                celluleLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(0, 35, Short.MAX_VALUE)
            );
            celluleLayout.setVerticalGroup(
                celluleLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(0, 33, Short.MAX_VALUE)
            );
     
            org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(layout.createSequentialGroup()
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                            .addContainerGap()
                            .add(jPanel3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .add(layout.createSequentialGroup()
                            .add(52, 52, 52)
                            .add(ledVerte, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(26, 26, 26)
                            .add(ledRouge, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .add(layout.createSequentialGroup()
                            .add(29, 29, 29)
                            .add(jPanel5, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .add(layout.createSequentialGroup()
                            .addContainerGap()
                            .add(cellule, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                    .addContainerGap())
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(layout.createSequentialGroup()
                    .addContainerGap()
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(ledRouge, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(ledVerte, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(jPanel3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                    .add(jPanel5, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(58, 58, 58)
                    .add(cellule, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(60, 60, 60))
            );
     
            pack();
        }// </editor-fold>                        
     
    private void celluleMouseEntered(java.awt.event.MouseEvent evt) {                                     
        run("sirene.wav");
    }                                    
     
    private void codeCarteActionPerformed(java.awt.event.ActionEvent evt) {                                          
        this.ledRouge.setBackground(new Color(255, 0, 0));
    }                                         
     
        /**
        * @param args the command line arguments
        */
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new BorneMAE().setVisible(true);
                }
            });
        }
     
        // Variables declaration - do not modify                     
        private javax.swing.JPanel cellule;
        private javax.swing.JComboBox codeCarte;
        private javax.swing.JPanel jPanel3;
        private javax.swing.JPanel jPanel4;
        private javax.swing.JPanel jPanel5;
        private javax.swing.JLabel lcdLigne1;
        private javax.swing.JLabel lcdLigne2;
        private javax.swing.JPanel ledRouge;
        private javax.swing.JPanel ledVerte;
        // End of variables declaration                   
     
     
    }
    Et le code de ma classe WavPlayer :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    package Borne;
     
    import java.io.*;
    import javax.sound.sampled.*;
     
    public class WavPlayer extends Thread {
        private String filename;
        private int nBytesRead = 0;
        private byte[] abData = new byte[524288];    // 128Kb
        private AudioInputStream audioInputStream = null;
        private AudioFormat format = null;
        private SourceDataLine auline = null;
     
        public WavPlayer(String wavfile) {
                filename = wavfile;
        }
     
        @Override
        public void run() {
            try {
                File soundFile = new File(filename);
                audioInputStream = AudioSystem.getAudioInputStream(soundFile);
                format = audioInputStream.getFormat();
                DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
                auline = (SourceDataLine) AudioSystem.getLine(info);
                auline.open(format);
                auline.start();
                while (nBytesRead != -1) {
                    nBytesRead = audioInputStream.read(abData, 0, abData.length);
                    if (nBytesRead >= 0)
                            auline.write(abData, 0, nBytesRead);
                }
            } catch (Exception e) {
                    e.printStackTrace();
                    return;
            } finally {
                if (auline != null) {
                    auline.drain();
                    auline.close();
                }
            }
        }
    }
    Merci d'avance !

  4. #4
    Membre Expert
    Avatar de supersnail
    Homme Profil pro
    Inscrit en
    Novembre 2006
    Messages
    1 719
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Novembre 2006
    Messages : 1 719
    Par défaut
    bah forcément... tu définis pas la méthode run(string) mais run()...

    De plus,on code pas un thread comme ça... (enfin jamais vu ça...)
    Donc,ta waveplayer modifiée:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
     
    package Borne;
     
    import java.io.*;
    import javax.sound.sampled.*;
     
    public class WavPlayer implements Runnable {
        private String filename;
        private int nBytesRead = 0;
        private byte[] abData = new byte[524288];    // 128Kb
        private AudioInputStream audioInputStream = null;
        private AudioFormat format = null;
        private SourceDataLine auline = null;
     
        public WavPlayer(String wavfile) {
                filename = wavfile;
        }
     
        @Override
        public void run() {
            try {
                File soundFile = new File(filename);
                audioInputStream = AudioSystem.getAudioInputStream(soundFile);
                format = audioInputStream.getFormat();
                DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
                auline = (SourceDataLine) AudioSystem.getLine(info);
                auline.open(format);
                auline.start();
                while (nBytesRead != -1) {
                    nBytesRead = audioInputStream.read(abData, 0, abData.length);
                    if (nBytesRead >= 0)
                            auline.write(abData, 0, nBytesRead);
                }
            } catch (Exception e) {
                    e.printStackTrace();
                    return;
            } finally {
                if (auline != null) {
                    auline.drain();
                    auline.close();
                }
            }
        }
    }
    et l'event qui l'appelle:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
    private void celluleMouseEntered(java.awt.event.MouseEvent evt) {                                     
        WavPlayer wav = new Wavplayer("le_fichier_a_lire");
        Thread t = new Thread(wav);
        t.start();
    }
    et voilà

  5. #5
    Membre confirmé
    Profil pro
    Inscrit en
    Juin 2008
    Messages
    77
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2008
    Messages : 77
    Par défaut
    Ca marche !

    Merci beaucoup pour ton aide supernsail, bonne fin de week end

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [Débutant] Jouer un son wav avec fichier intégré dans les ressources
    Par Claude_Azoulai dans le forum C#
    Réponses: 5
    Dernier message: 29/05/2013, 14h01
  2. [AC-2003] Jouer un son Wav une fois dans un timer
    Par Hamadokh dans le forum IHM
    Réponses: 1
    Dernier message: 25/09/2009, 20h07
  3. Jouer plusieurs sons wav en simultanée
    Par inertia dans le forum MFC
    Réponses: 5
    Dernier message: 12/05/2005, 14h46
  4. Créer un champ contenant un son wav dans une base Paradox
    Par Grandad95 dans le forum Bases de données
    Réponses: 4
    Dernier message: 21/07/2004, 17h00
  5. Existe t'il un composant permettant de jouer un son wav?
    Par scorpiwolf dans le forum C++Builder
    Réponses: 5
    Dernier message: 20/06/2002, 14h10

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo