hey everyone,
can someone help me on this problem
i have a Timer and a JButton in my application, and everytime i click on the button
it will play the sound file, however everytime it play the sound, the Timer stop
and the Timer will run again after playing the sound
i want the Timer to continue running while playing the Sound
btw this is the code(for testing purpose)
import java.io.*; import javax.sound.sampled.*; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JButton; import javax.swing.Timer; public class timer extends JFrame implements ActionListener{ private Timer myTimer; private int jam=0, menit=0, detik=0; private JLabel mylabel; private JButton sound; public timer(){ this.setLayout(new FlowLayout()); mylabel = new JLabel("time"); this.add(mylabel); sound = new JButton("Sound"); sound.addActionListener(this); this.add(sound); this.setVisible(true); this.setSize(150,100); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); myTimer = new Timer(100, new TimerHandler()); myTimer.start(); } public void actionPerformed(ActionEvent event) { if(event.getSource()==sound) { try { System.out.println("start sound"); playSound(); System.out.println("finish sound"); } catch (Exception e) {} System.out.println("You click sound"); } } public void playSound() throws Exception { File soundFile = new File("03.wav"); // edit this line for another sound file Clip clickClip = AudioSystem.getClip(); AudioInputStream ais = AudioSystem.getAudioInputStream(soundFile); clickClip.open(ais); clickClip.start(); int nBytesRead = 0; byte[] abData = new byte[524288]; System.out.println("play"); try { while (nBytesRead != -1) { nBytesRead = ais.read(abData, 0, abData.length); } } catch(Exception e) {} finally { clickClip.drain(); clickClip.close(); } } private class TimerHandler implements ActionListener{ private String buff; private void write(){ buff = String.format(" %s%3d :%3d :%3d", "Timer: ",jam, menit, detik); mylabel.setText(buff); } public void actionPerformed(ActionEvent e) { detik++; if(detik==60){ detik=0; menit++; } if(menit==60){ menit=0; jam++; } if(jam==24){ jam=0; } this.write(); } } public static void main(String[] args) { new timer(); } }
can someone help me to accomplish the task?
sorry for the bad English