Re: Timer
rainny wrote:
Hi, I have an application which is mainly play the sound program.
Whenever I play the sound, I want the time is counted. And when the
sound is stop, the time is stop count.
Can anybody suggest me how to add the timer to the application so that
I can get the start time and the end time of the sound whenever the
sound is play?
Thanks you for helping.
Regards
Rainny
The first reply, which was by Roedy Green, answered your question.
....
long startTime, stopTime;
....
// start button pressed stuff
startTime = System.currentTimeMillis();
// more start button pressed stuff
....
....
// stop button pressed stuff
stopTime = System.currentTimeMillis ();
// more stop button pressed stuff
....
// length of play, in milliseconds = stopTime - startTime
// format according to your needs
I suspect that what you really want is to display the running time while the clip is playing. Take another look at the code posted by Andrew Thompson. In particular, pay attention to the part that controls the progress bar. Here are some of the relevant lines of that code:
....
Thread t;
boolean active = true;
....
t = new Thread(this);
t.start();
....
public void run() {
int time = 80;
while(active) {
try {
....
}
}
progressBar.setValue( (int)clip.getMicrosecondPosition() );
}
t.sleep(time); } catch(InterruptedException ie)
{ // awake and continue }
}
}
public void setActive(boolean active) { this.active = active; }
public void shutDown() { setActive(false); System.exit(0); }
(My copy and past did not duplicate the indentation.)
Where Andrew Thompson's code updates the progress bar, yours would update the time display.
For more information, go to java.sun.com, and look at the API.