Results 1 to 1 of 1
  1. #1
    JagFig is offline Member
    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0

    Default How to clear Audio Clips from memory

    I am using an ArrayList to store all of the audio clips in my code.

    I honestly don't know too much about audio in Java, so I'm not really sure how to go about
    clearing the Clips from memory once they are done playing.

    The .flush() and .drain() methods of the DataLine class sound promising, but again, I'm not entirely sure what they do.

    This is the code I have so far:

    Java Code:
    import java.io.*;
    import javax.sound.sampled.*;
    import java.util.*;
    
    class SoundEffectArray {
       
       private static ArrayList<Clip> clips = new ArrayList<Clip>();
       
       	public static void addSound(String soundFileName) {
       		try {
             AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File(soundFileName));
             clips.add(AudioSystem.getClip());
             
             Clip currClip = clips.get(clips.size() - 1);
             
             currClip.open(audioInputStream);
             currClip.start();
             
          } catch (UnsupportedAudioFileException e) {
             e.printStackTrace();
          } catch (IOException e) {
             e.printStackTrace();
          } catch (LineUnavailableException e) {
             e.printStackTrace();
          }
       	}
       	
       	public static void update() {
       		ArrayList<Integer> cleanup = new ArrayList<Integer>();
       		
       		for(int i = 0; i < clips.size(); i++) {
       			Clip currClip = clips.get(i);
       			if (currClip.getMicrosecondPosition() >= currClip.getMicrosecondLength()) {
       				cleanup.add(i);
       			}
       		}
       		for(int i = 0; i < cleanup.size(); i++) {
       			
       			//I want to clear the clip from memory here
       			
       			clips.remove((int) cleanup.get(i));
       		}
       	}
    }
    Last edited by JagFig; 11-01-2012 at 05:11 AM.

Similar Threads

  1. Replies: 2
    Last Post: 08-20-2012, 07:38 PM
  2. Replies: 8
    Last Post: 08-11-2012, 08:18 PM
  3. Playing audio clips
    By robs in forum Threads and Synchronization
    Replies: 24
    Last Post: 06-26-2012, 08:14 PM
  4. Video clips in side bar start playing automagically.
    By JosAH in forum Suggestions & Feedback
    Replies: 10
    Last Post: 05-26-2011, 10:01 AM
  5. Replies: 2
    Last Post: 08-21-2008, 08:33 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
  •