need help with Timer and sound
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)
Code :
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 :(
Re: need help with Timer and sound
Swing operates on a single thread called the EDT, so any long processes a program has should be placed on a separate thread so it doesn't clog up the EDT. In your case, the playing of the sound clip could be clogging up the EDT so any calls by the timer will not be run until that process has ended. To overcome this you can play your sound on a seperate thread
Code :
if(event.getSource()==sound) {
Runnable runner = new Runnable(){
public void run(){
playSound();
}
}
(new Thread(runner)).start();
}
If you don't know what any of the code above means, I recommend reading about Concurrency
Re: need help with Timer and sound
thx for the explanation copeg :D,
so java need to create new separate thread to handle multiple task at once
btw what happens with the created thread after it finished it's task?
will it stay in the memory until destroyed or will it destroy itself?
and another question, can java play music file like mp3 or video file like mpeg without adding JMF?
just wondering if it can be done ;D
Re: need help with Timer and sound
Quote:
so java need to create new separate thread to handle multiple task at once
In a way, yes. Threads are linear, so if you don't want to clog one up you should start another.
Quote:
btw what happens with the created thread after it finished it's task?
will it stay in the memory until destroyed or will it destroy itself?
Once the run() method of Runnable exits, the thread stops. Read the link I posted above for more info
Quote:
and another question, can java play music file like mp3 or video file like mpeg without adding JMF?
just wondering if it can be done ;D
I haven't done much movie playing in java, so defer to anyone else with experience
Re: need help with Timer and sound
well once again i thank u for your explanation :D