我有一个带有两个按钮的GUI Java框架:开始按钮和停止按钮。
开始按钮(可能会在按下.wav声音文件时不断播放),停止按钮(可能会停止播放.wav声音文件)。当我按开始按钮时,开始播放可以正常工作,但是,停止按钮无法正常工作或对停止播放.wav文件没有任何作用。正如您在我的代码中看到的那样,我已经尝试了很多事情。任何帮助将不胜感激。
从这里开始固定
package newframe;
import java.io.File;
import java.io.*;
import static java.time.Clock.system;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import java.awt.*;
import javax.swing.*;
/** *
* @author gamalt
*/
public class NewJFrame extends javax.swing.JFrame implements Runnable {
/**
* Creates new form NewJFrame
*/
Clip clip;
volatile int x=5;
static int xx=0;
int y=200;
public Thread thread= null;
public NewJFrame() {
initComponents();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thread = new Thread(this,"gamal");
}
public void run(){
System.out.println("you are in run");
x=20;
}
/**
* 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() {
jButton1 = new javax.swing.JButton();
START = new javax.swing.JButton();
STOP = new javax.swing.JButton();
jColorChooser1 = new javax.swing.JColorChooser();
jButton1.setText("jButton1");
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
START.setBackground(new java.awt.Color(0, 204, 153));
START.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
START.setForeground(new java.awt.Color(0, 51, 51));
START.setText("START");
START.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
STARTActionPerformed(evt);
}
});
STOP.setBackground(new java.awt.Color(255, 0, 0));
STOP.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
STOP.setText("STOP");
STOP.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
STOPActionPerformed(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()
.addComponent(
jColorChooser1,
javax.swing.GroupLayout.PREFERRED_SIZE,
0,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(108, 108, 108)
.addComponent(
STOP,
javax.swing.GroupLayout.PREFERRED_SIZE,
98,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(
javax.swing.LayoutStyle.ComponentPlacement.RELATED,
216,
Short.MAX_VALUE)
.addComponent(
START,
javax.swing.GroupLayout.PREFERRED_SIZE,
117,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(82, 82, 82))
);
layout.setVerticalGroup(
layout
.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
layout
.createSequentialGroup()
.addContainerGap()
.addGroup(
layout
.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(
jColorChooser1,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(
layout
.createSequentialGroup()
.addGap(93, 93, 93)
.addGroup(
layout
.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(
START,
javax.swing.GroupLayout.PREFERRED_SIZE,
61,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(
STOP,
javax.swing.GroupLayout.PREFERRED_SIZE,
61,
javax.swing.GroupLayout.PREFERRED_SIZE)))).addContainerGap(78, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
// FIXED FROM HERE DOWN!!!!
private void STARTActionPerformed(java.awt.event.ActionEvent evt){
try {
clip = AudioSystem.getClip();
//AudioInputStream inputStream = AudioSystem.getAudioInputStream(new File("src/myrecord3.wav"));
// AudioInputStream inputStream = AudioSystem.getAudioInputStream(NewJFrame.class.getResourceAsStream("myrecord3.wav"));
BufferedInputStream myStream = new BufferedInputStream(getClass().getResourceAsStream("myrecord3.wav"));
// just addes
AudioInputStream audio2 = AudioSystem.getAudioInputStream(myStream); //just added
//AudioInputStream inputStream = AudioSystem.getAudioInputStream(new File("src/myrecord3.wav"));
// clip.open(inputStream);
clip.open(audio2);
xx=5;
// clip.start();
// clip.loop(Clip.LOOP_CONTINUOUSLY);
System.out.println("hi");
// clip.drain();
while(xx == 5){
clip.setFramePosition(0);
clip.loop(0);
clip.start();
// System.out.println("yes againg");
// while(clip.isActive())
// x=y;
Thread.sleep(10000);}
} catch (Exception e) {
System.err.println(e.getMessage());
}
// TODO add your handling code here:
}
private void STOPActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here
xx=200; //stop the thread
// System.exit(0);
// Thread.yield();
//thread.start();
if(clip == null)
System.out.println("clip is null");
try {
Thread.currentThread().interrupt();
System.out.println("stop");
clip.close();
} catch(Exception e) {
System.out.println("something is wrong");
}
}
/**
* @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(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
catch(Exception e) {}
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton START;
private javax.swing.JButton STOP;
private javax.swing.JButton jButton1;
private javax.swing.JColorChooser jColorChooser1;
// End of variables declaration
}