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 5 of 5

Thread: need help with Timer and sound

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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)

    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


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,260
    Thanks
    176
    Thanked 818 Times in 761 Posts
    Blog Entries
    5

    Default 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

    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

  3. The Following User Says Thank You to copeg For This Useful Post:

    amahara (February 17th, 2010)

  4. #3
    Junior Member
    Join Date
    Feb 2010
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: need help with Timer and sound

    thx for the explanation copeg ,
    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

  5. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,260
    Thanks
    176
    Thanked 818 Times in 761 Posts
    Blog Entries
    5

    Default Re: need help with Timer and sound

    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.

    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

    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

  6. The Following User Says Thank You to copeg For This Useful Post:

    amahara (February 18th, 2010)

  7. #5
    Junior Member
    Join Date
    Feb 2010
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: need help with Timer and sound

    well once again i thank u for your explanation

Similar Threads

  1. How to Use Timer in Java
    By neo_2010 in forum Java SE API Tutorials
    Replies: 2
    Last Post: August 6th, 2013, 09:49 AM
  2. Timer?
    By TimW in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: October 27th, 2009, 07:43 AM
  3. Difference between Speech API and Sound API
    By zeeshanmirza in forum Java SE APIs
    Replies: 1
    Last Post: October 22nd, 2009, 12:22 AM
  4. How to display sound signal waveforms continously?
    By ces_31 in forum Java Theory & Questions
    Replies: 4
    Last Post: August 10th, 2009, 09:43 AM
  5. Help - Swing Timer, 2 KeyEvents
    By Gheta in forum AWT / Java Swing
    Replies: 2
    Last Post: July 29th, 2009, 02:46 PM