Results 1 to 2 of 2

Thread: Closing a clip

  1. #1
    elamre is offline Member
    Join Date
    Jan 2012
    Posts
    23
    Rep Power
    0

    Default Closing a clip

    Hello, i have a big memmory leak in my program, and ive found the problem. But i cant seem to fix it correctly. Im making a towerdefence game, every tower has his own thread. Then as soon as the tower will shoot, it will play a sound. Here it goes wrong. I have to close my clip (otherwise my game will run out of memmory) but i have to close it only AFTER the sound is done. Here is my playSound function:
    Java Code:
    	private void playSound(String filename) {
    		 File soundFile = new File(filename);
    		 
    			AudioInputStream sound = null;
    			try {
    				sound = AudioSystem.getAudioInputStream(soundFile);
    			} catch (UnsupportedAudioFileException e) {
    				e.printStackTrace();
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    		    DataLine.Info info = new DataLine.Info(Clip.class, sound.getFormat());
    		    Clip clip = null;
    			try {
    				clip = (Clip) AudioSystem.getLine(info);
    			} catch (LineUnavailableException e) {
    				e.printStackTrace();
    			}
    
    			try {
    				clip.open(sound);
    				sound.close();
    			} catch (LineUnavailableException e) {
    				e.printStackTrace();
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    
    		    clip.addLineListener(new LineListener(){public void update(LineEvent event){}});
    
    		    clip.start();
    		    try {
    				Thread.sleep(100);
    			} catch (InterruptedException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    		    clip.close();
    		    clip.flush();
    	}
    I have tried several solutions (clip.isOpen, clip.isRunning but nothing seems to solve my case)

    Any help on closing the clip AFTER its done playing?

  2. #2
    elamre is offline Member
    Join Date
    Jan 2012
    Posts
    23
    Rep Power
    0

    Default Re: Closing a clip

    Also if im increasing the delay it will work. But not the way i want to (now it shoots 500 ms after you hear the sound. And if you are upgrading a tower the whole game will freeze, and the delay isnt long enough for that.

Similar Threads

  1. java CLIP
    By nikkka in forum New To Java
    Replies: 0
    Last Post: 04-21-2011, 08:52 AM
  2. Fill in rectangle partially (clip?)
    By OBLITERATOR in forum New To Java
    Replies: 1
    Last Post: 03-07-2010, 04:59 AM
  3. sound clip
    By f_the_cook in forum Advanced Java
    Replies: 4
    Last Post: 10-17-2008, 05:19 PM
  4. How to Clip an Image
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-23-2008, 11:24 PM
  5. How do i play an audio clip from a server?
    By JavaJenius in forum Networking
    Replies: 1
    Last Post: 03-08-2008, 06:40 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •