Results 1 to 2 of 2
  1. #1
    0nix is offline Member
    Join Date
    Sep 2013
    Rep Power

    Default Help me out, Game sound delay

    Java Code:
    import javax.sound.sampled.AudioInputStream;
    import javax.sound.sampled.AudioSystem;
    import javax.sound.sampled.Clip;
    import javax.sound.sampled.DataLine.Info;
    import javax.sound.sampled.LineEvent;
    import javax.sound.sampled.LineEvent.Type;
    import javax.sound.sampled.LineListener;
    import javax.sound.sampled.LineUnavailableException;
    import javax.sound.sampled.Mixer;
    import javax.sound.sampled.FloatControl;
    import static javax.sound.sampled.LineEvent.Type.OPEN;
    import static javax.sound.sampled.LineEvent.Type.STOP;
    public class GameSoundPlayer implements LineListener {
    	private Mixer mixer;
    	private boolean errorLoadingMixer;
    			private int delay;
    	public GameSoundPlayer() {
    		//get the system default mixer
    		mixer = AudioSystem.getMixer(null);
    		if(mixer != null) {
    			try {;
    			} catch (LineUnavailableException e) {
    		errorLoadingMixer = true;
    		System.err.println("Error initlizing mixer!");
    	public void appendSound(InputStream sound, int delay) throws Exception { 
    		//check that our mixer has been loaded correctly
    		if(errorLoadingMixer) {
    			throw new Exception("Mixer required!");
    		 AudioInputStream stream = AudioSystem.getAudioInputStream(sound);
    		 Info lineInformation = new Info(Clip.class, stream.getFormat());
    		//check if the mixer support the audio file
    		if(mixer.isLineSupported(lineInformation)) {
    			this.delay = delay;
    			Clip clip = (Clip) AudioSystem.getLine(lineInformation);
    	public void update(LineEvent line) {
    	try {
    		Type type = line.getType();
    		Object source = line.getSource();
    		if(!(source instanceof Clip)) {
    		Clip clip = (Clip) source;
    		if(type == OPEN) {
    			if (delay > 0) {
    		} else if(type == STOP) {
    		 } catch (Exception e) {

    I'm trying to delay specific sounds according to the int delay, however it also delays every other sound with lower delay played at the same time:

    Java Code:
    			if (delay > 0) {

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Washington, DC
    Rep Power

    Default Re: Help me out, Game sound delay

    I don't really know what this code does because you haven't posted an SSCCE, but assuming this is being run on the EDT, calling sleep() is a very bad idea.
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

Similar Threads

  1. Getting sound from sound card
    By crikey in forum Advanced Java
    Replies: 1
    Last Post: 06-18-2013, 10:07 AM
  2. Applying Sound to Java Game
    By VTechJava in forum New To Java
    Replies: 2
    Last Post: 04-26-2013, 06:11 PM
  3. Where can I create sound for a computer game?
    By rajkobie in forum New To Java
    Replies: 2
    Last Post: 05-12-2011, 10:07 AM
  4. Problems with sound in java and USB sound card
    By marblecatdog in forum New To Java
    Replies: 1
    Last Post: 04-19-2011, 01:02 PM
  5. Read URL with delay
    By Eviler in forum Advanced Java
    Replies: 5
    Last Post: 04-04-2011, 08:16 AM

Posting Permissions

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