Hello. This is my first post on this forum. I wrote a workout timer similar to this one that I wrote in Win32 C.
The program is all but done however I couldn't figure out how to sync the sounds when the work time ends and the rest time begins. The problem is when the sound plays there is a noticeable delay before the timer starts to count down. When it does start to count down, the numbers change very quickly before it catches up and then the numbers change at a normal rate.
Here is the code that generates the sound:
import java.io.File; import javax.sound.sampled.AudioFormat; import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.DataLine; import javax.sound.sampled.SourceDataLine; public class PlaySounds { private String fileName = null; public PlaySounds (String fName) { fileName = fName; playFile(); } private void playFile() { int EXTERNAL_BUFFER_SIZE = 524288; File soundFile = new File(fileName); AudioInputStream audioInputStream = null; try { audioInputStream = AudioSystem.getAudioInputStream(soundFile); } catch (Exception e) { // do nothing with it } AudioFormat format = audioInputStream.getFormat(); SourceDataLine auLine = null; DataLine.Info info = new DataLine.Info(SourceDataLine.class, format); try { auLine = (SourceDataLine) AudioSystem.getLine(info); auLine.open(format); } catch (Exception e) { // do nothing with it } auLine.start(); int nBytesRead = 0; byte[] abData = new byte[EXTERNAL_BUFFER_SIZE]; try { while (nBytesRead != -1) { nBytesRead = audioInputStream.read(abData, 0, abData.length); if (nBytesRead >= 0) { auLine.write(abData, 0, nBytesRead); } } } catch (Exception e) { // do nothing with it } finally { auLine.drain(); auLine.close(); } } }
And here is the action listener that runs the timer and calls the sound code:
private class StartTimerHandler implements ActionListener { public void actionPerformed (ActionEvent event) { if (clockTimer == null) { clockTimer = new Timer(TIME_INTERVAL, new TimeHandler()); if (timeParam.getWorkMin() >= 0 || timeParam.getWorkSec() > 0) { isWorkTimerOn = true; PlaySounds playSound = new PlaySounds("boxing_bell.wav"); clockTimer.start(); } else JOptionPane.showMessageDialog(null, "Work time cannot be 0", "Timer Error", JOptionPane.WARNING_MESSAGE); } } // public void actionPerformed (ActionEvent event) } // private class StartTimerHandler implements ActionListener
***EDIT**** Here is the TimerHandler. It has been a while since I looked at the code.
private class TimeHandler implements ActionListener { public void actionPerformed (ActionEvent event) { int workMinutes, workSeconds; int restMinutes, restSeconds; // int numRounds; workMinutes = timeParam.getWorkMin(); workSeconds = timeParam.getWorkSec(); restMinutes = timeParam.getRestMin(); restSeconds = timeParam.getRestSec(); if (isWorkTimerOn) if (workSeconds > 0) { timeParam.setWorkSec(workSeconds - 1); updateWorkSecLabel(timeParam.getWorkSec()); } // if (workSeconds > 0) else if (workMinutes > 0) { timeParam.setWorkMin(workMinutes - 1); timeParam.setWorkSec(59); updateWorkMinLabel(timeParam.getWorkMin()); updateWorkSecLabel(timeParam.getWorkSec()); } // else if (workMinutes > 0) // Work timer = 00:00 else if (timeParam.getCurrentRound() != 0) { PlaySounds playSound = new PlaySounds("boxingbellmulti.wav"); isWorkTimerOn = false; isRestTimerOn = true; timeParam.resetWorkTime(); updateWorkMinLabel(timeParam.getWorkMin()); updateWorkSecLabel(timeParam.getWorkSec()); } // no more rounds stop timer else clockTimer.stop(); if (isRestTimerOn) { if (restSeconds > 0) { timeParam.setRestSec(restSeconds - 1); updateRestSecLabel(timeParam.getRestSec()); } // if (restSeconds > 0) else if (restMinutes > 0) { timeParam.setRestMin(restMinutes - 1); timeParam.setRestSec(59); updateRestMinLabel(timeParam.getRestMin()); updateRestSecLabel(timeParam.getRestSec()); } // else if (restMinutes > 0) else if (timeParam.getCurrentRound() != timeParam.getInitialNumberRounds()) { PlaySounds playSound = new PlaySounds("boxing_bell.wav"); isWorkTimerOn = true; isRestTimerOn = false; timeParam.resetRestTime(); timeParam.setCurrentRound(timeParam.getCurrentRound() + 1); updateRestMinLabel(timeParam.getRestMin()); updateRestSecLabel(timeParam.getRestSec()); updateRoundsLabel(timeParam.getCurrentRound()); } else { clockTimer.stop(); timeParam.resetWorkTime(); timeParam.resetRestTime(); timeParam.resetRounds(); clockTimer = null; isRestTimerOn = false; PlaySounds playSound = new PlaySounds("boxingbellmulti.wav"); } } // if (isRestTimerOn) } // public void actionPerformed (ActionEvent event) } // private class TimeHandler implements ActionListener
***END EDIT***
Any help or suggestions would be greatly appreciated. I've had two courses in Java, but of course they didn't cover sound and sync-ing. I had to search all over the web to find out how to get Java to play wav files.
Thanks
Curt