Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 1 of 1

Thread: Sync movement with sound?

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Sync movement with sound?

    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
    Last edited by cl2606; June 2nd, 2011 at 08:44 PM.


Similar Threads

  1. 3D camera movement
    By macko in forum Java Theory & Questions
    Replies: 2
    Last Post: May 17th, 2011, 08:53 AM
  2. Force Sync on Graphics2D?
    By Gerp in forum Java Theory & Questions
    Replies: 2
    Last Post: April 13th, 2011, 10:13 PM
  3. JFrame Movement Listening
    By aussiemcgr in forum AWT / Java Swing
    Replies: 3
    Last Post: March 9th, 2011, 11:48 AM
  4. movement
    By mlan in forum Java Theory & Questions
    Replies: 4
    Last Post: February 15th, 2010, 10:57 PM
  5. sync two diffrent kind of threads?
    By adamruss in forum Threads
    Replies: 1
    Last Post: January 10th, 2010, 08:59 PM