Buen día a todos los del foro, necesito de su apreciable ayuda para adaptar el siguiente código que reproduce audio al iniciar la aplicación y que vez de eso solo se reproduzca una pista diferente para cada botón, de antemano agradezco cualquier apoyo. El código es el siguiente:
import com.panamahitek.ArduinoException;
import com.panamahitek.PanamaHitek_Arduino;
import java.util.logging.Level;
import java.util.logging.Logger;
import jssc.SerialPortException;
import java.io.*;
import sun.audio.*;
public class JavaTX extends javax.swing.JFrame {
//Se crea la instancia de la librería PanamaHitek_Arduino
PanamaHitek_Arduino ino = new PanamaHitek_Arduino();
public JavaTX() {
initComponents();
try {
//Se inicia la comunicación con el Puerto Serie
ino.arduinoTX("COM4", 9600);
} catch (ArduinoException ex) {
Logger.getLogger(JavaTX.class.getName()).log(Level.SEVERE, null, ex);
}
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Arduino Serial Switch | Panama Hitek");
jButton1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
jButton1.setText("Encender");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
jButton2.setText("Apagar");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(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()
.addGap(32, 32, 32)
.addComponent(jButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(38, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(33, 33, 33)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jButton2))
.addContainerGap(36, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
try {
ino.sendData("1");
} catch (ArduinoException | SerialPortException ex) {
Logger.getLogger(JavaTX.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
//Se hace el envío del String "0"
try {
ino.sendData("0");
} catch (ArduinoException | SerialPortException ex) {
Logger.getLogger(JavaTX.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static void main(String args[]) throws Exception {
String sonido1 = "C:/users/jesus/desktop/sonido/NombreAudio.wav";
String sonido2 = "C:/users/jesus/desktop/sonido/NombreAudio.wav";
InputStream in = new FileInputStream(sonido1);
InputStream in2 = new FileInputStream(sonido2);
AudioStream audio1 = new AudioStream(in);
AudioStream audio2 = new AudioStream(in2);
AudioPlayer.player.start(audio1);
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new JavaTX().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
// End of variables declaration
}