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

    Default Help me out, Game sound delay

    Java Code:
    import java.io.InputStream;
    
    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 {
    				mixer.open();
    				return;
    			} catch (LineUnavailableException e) {
    				e.printStackTrace();
    			}
    		}
    		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);
    			clip.addLineListener(this);
    			clip.open(stream);
    				
    		}
    	}
    	
    
    	@Override
    	public void update(LineEvent line) {
    	try {
    		Type type = line.getType();
    		Object source = line.getSource();
    		
    		if(!(source instanceof Clip)) {
    			return;
    		}
    		Clip clip = (Clip) source;
    		
    		if(type == OPEN) {
    			if (delay > 0) {
    				Thread.sleep(delay);
    			}
    			clip.start();
    			
    		} else if(type == STOP) {
    			clip.close();
    			line.getLine().close();
    		}
    		 } catch (Exception e) {
    			player.interrupt();
    			e.printStackTrace();
    		}
    	}
    	
    }

    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) {
    				Thread.sleep(delay);
    			}

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

    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 - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Getting sound from sound card
    By crikey in forum Advanced Java
    Replies: 1
    Last Post: 06-18-2013, 09:07 AM
  2. Applying Sound to Java Game
    By VTechJava in forum New To Java
    Replies: 2
    Last Post: 04-26-2013, 05: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, 09: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, 12:02 PM
  5. Read URL with delay
    By Eviler in forum Advanced Java
    Replies: 5
    Last Post: 04-04-2011, 07: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
  •