Java - Mantener la reproducción de un audio durante dos ventanas Splash

   
Vista:

Mantener la reproducción de un audio durante dos ventanas Splash

Publicado por Alejandro (16 intervenciones) el 10/02/2018 04:24:27
Buenas, tengo un código de un juego y tengo implementadas dos ventanas splash que aparecen antes de aparecer la ventana del juego, la primera tiene un archivo de audio y quisiera que el mismo archivo de audio se mantenga reproduciendo hasta que desaparezcan las dos ventanas, sin embargo no puedo hacerlo, ya que o se para al alcanzar la primera Jprogressbar el 100, o se sigue reproduciendo tanto en la primera como en la segunda ventana y los sonidos se mezclan, el sonido se para al alcanzar el 100 en la barra de progreso. ¿Pueden ayudarme?

Este es el codigo de la primera splash
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
package splash;
 
import com.sun.awt.AWTUtilities;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JProgressBar;
import splash.Names;
 
/**
 *
 * @author Alejandro
 */
public class Splash extends javax.swing.JFrame {
 
    /**
     * Creates new form Splash
     */
    double i = 50, j = 1;
    Cargar hilo;
    Sonido s;
 
    public Splash() {
        initComponents();
        ajustarImagen();
        iniciar();
    }
 
    public void ajustarImagen() {
        ImageIcon logo = new ImageIcon(getClass().getResource("/Imagenes/uce_logo.jpg"));
        ImageIcon icono = new ImageIcon(logo.getImage().getScaledInstance(imagen.getWidth(), imagen.getHeight(), Image.SCALE_DEFAULT));
        imagen.setIcon(icono);
 
    }
 
    public void iniciar() {
        setLocationRelativeTo(null);
        progreso.setVisible(true);
        hilo = new Cargar(getProgreso());
        s = new Sonido();
        cargarMusica();
        hilo.start();
        hilo = null;
    }
 
    public void cargarMusica() {
        try {
            Thread.sleep(700);
        } catch (InterruptedException e) {
 
        }
    }
 
    public javax.swing.JProgressBar getProgreso() {
        return progreso;
    }
 
    public void setProgreso(javax.swing.JProgressBar progreso) {
        this.progreso = progreso;
    }
 
    /**
     * 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() {
 
        progreso = new javax.swing.JProgressBar();
        imagen = new javax.swing.JLabel();
 
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setUndecorated(true);
 
        progreso.setForeground(new java.awt.Color(51, 102, 255));
        progreso.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                progresoStateChanged(evt);
            }
        });
 
        imagen.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
 
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(imagen, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(progreso, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 757, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(imagen, javax.swing.GroupLayout.PREFERRED_SIZE, 445, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(progreso, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
 
        pack();
    }// </editor-fold>                        
 
    private void progresoStateChanged(javax.swing.event.ChangeEvent evt) {
        // TODO add your handling code here:
        if (progreso.getValue() == i) {
            if (j != 101) {
                AWTUtilities.setWindowOpacity(this, Float.valueOf((100 - j) / 100 + "f"));
                i++;
                j += 2;
            }
        }
 
        if (progreso.getValue() == 100) {
            Names t = new Names();
            s.apagar();
            t.setVisible(true);
            this.dispose();;
        }
 
 
    }
 
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Splash.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Splash.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Splash.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Splash.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
 
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Splash().setVisible(true);
            }
        });
    }
 
    // Variables declaration - do not modify                     
    private javax.swing.JLabel imagen;
    private javax.swing.JProgressBar progreso;
    // End of variables declaration                   
}

La segunda ventana splash:

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
package splash;
 
import com.sun.awt.AWTUtilities;
import java.awt.Color;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import splash.Splash;
 
/**
 *
 * @author Alejandro
 */
public class Names extends javax.swing.JFrame {
 
    /**
     * Creates new form Test
     */
    double i = 50, j = 1;
    Cargar hilo;
    Sonido s;
 
    public Names() {
        initComponents();
        iniciar();
 
        this.getContentPane().setBackground(Color.orange);
    }
 
    public void cargarMusica() {
        try {
            Thread.sleep(700);
        } catch (InterruptedException e) {
 
        }
    }
 
    public void iniciar() {
        setLocationRelativeTo(null);
        progreso2.setVisible(false);
        hilo = new Cargar(getProgreso2());
        s = new Sonido();
        cargarMusica();
        hilo.start();
        hilo = null;
    }
 
    public javax.swing.JProgressBar getProgreso2() {
        return progreso2;
    }
 
    public void setProgreso(javax.swing.JProgressBar progreso2) {
        this.progreso2 = progreso2;
    }
 
    /**
     * 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() {
 
        jLabel1 = new javax.swing.JLabel();
        texto = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        progreso2 = new javax.swing.JProgressBar();
 
        jLabel1.setText("jLabel1");
 
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setUndecorated(true);
 
        texto.setFont(new java.awt.Font("Times New Roman", 1, 22)); // NOI18N
        texto.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        texto.setText("FACULTAD DE INGENIERÍA ");
 
        jLabel6.setFont(new java.awt.Font("Times New Roman", 3, 13)); // NOI18N
        jLabel6.setText("Todos los derechos reservados.");
 
  progreso2.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                progreso2StateChanged(evt);
            }
        });
 
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(texto, javax.swing.GroupLayout.DEFAULT_SIZE, 733, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(35, 35, 35)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel7)
                                .addGap(75, 75, 75)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel4)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel5)))
                            .addComponent(jLabel2))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(jLabel6))
            .addGroup(layout.createSequentialGroup()
                .addGap(53, 53, 53)
                .addComponent(progreso2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(59, 59, 59)
                .addComponent(texto, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 82, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(jLabel3))
                .addGap(18, 18, 18)
                .addComponent(jLabel4)
                .addGap(18, 18, 18)
                .addComponent(jLabel5)
                .addGap(35, 35, 35)
                .addComponent(progreso2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(15, 15, 15)
                .addComponent(jLabel6)
                .addContainerGap())
        );
 
        pack();
    }// </editor-fold>                        
 
    private void progreso2StateChanged(javax.swing.event.ChangeEvent evt) {
        if (progreso2.getValue() == i) {
            if (j != 101) {
                AWTUtilities.setWindowOpacity(this, Float.valueOf((100 - j) / 100 + "f"));
                i++;
                j += 2;
            }
        }
        if (progreso2.getValue() == 100) {
            s.apagar();
            Prueba p = new Prueba();
            p.setVisible(true);
            this.dispose();;
        }
 
    }
 
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Names.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Names.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Names.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Names.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        //</editor-fold>
 
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Names().setVisible(true);
            }
        });
    }
 
    // Variables declaration - do not modify                     
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JProgressBar progreso2;
    private javax.swing.JLabel texto;
    // End of variables declaration                   
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

Mantener la reproducción de un audio durante dos ventanas Splash

Publicado por Tom (1223 intervenciones) el 10/02/2018 10:13:42
Lo que es un splash screen y como usarlo en java, está descrito aquí:

https://docs.oracle.com/javase/tutorial/uiswing/misc/splashscreen.html

No tiene mucho sentido (o ninguno) usar dos seguidas. En todo caso, haz un thread aparte que se encarge del sonido.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
-1
Comentar
Revisar política de publicidad