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

Thread: Help with java audio samples please!

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with java audio samples please!

    Hello!

    I just signed up to ask this question. I'm not very good at java at all, I'm just dabbling. I don't mean to be rude or anything I am just new and english is only my second language!

    My question is about audio in java. I have a class and I am trying to load two different audio samples and they will be played at two different conditions. My code is probably not being very efficient but I am trying!

    The problem I have is I can only get the first sound to play, the kick. I will post code and you can see. I will only post the code that is to do with the audio as the class is quite big, a gui! Here is my code:

     
    import javax.sound.sampled.AudioInputStream;
    import javax.sound.sampled.AudioSystem;
    import javax.sound.sampled.Clip;
    import javax.sound.sampled.DataLine;
    import javax.sound.sampled.LineEvent;
    import javax.sound.sampled.LineListener;
     
    import java.applet.Applet;
    import java.applet.AudioClip;
     
    public class Thing{
     
    private File soundFile = new File("C:\\Users\\Desktop\\Kick.wav");
    	private File soundFile2 = new File("C:\\Users\\Desktop\\Snare.wav");
     
    	private AudioInputStream sound;
    	private AudioInputStream sound2;
     
    	private Clip kick;
    	private Clip snare;
     
    	public void init() {
    		Container contentPane = this.getContentPane();
    		contentPane
    				.add(new JLabel(
    						"A window has been opened. Check your taskbar to see it. Refresh this page to reopen it."));
    		contentPane.repaint();
     
    		Registry.init();
    		String header = "JEthereal";
    		makeFrame(header);
    		initSynth();
    		initAudio();
    		initAudio2();
    	}
     
    public void initAudio()
    	{
    		try
    		{		
    			sound = AudioSystem.getAudioInputStream(soundFile);
    			DataLine.Info info = new DataLine.Info(Clip.class, sound.getFormat());
    		    kick = (Clip) AudioSystem.getLine(info);
    		    kick.open(sound);
     
    		}
    		catch (Exception exc)
            {
                exc.printStackTrace();
            }
     
    	}
     
    	public void initAudio2()
    	{
    		try
    		{		
    			sound2 = AudioSystem.getAudioInputStream(soundFile2);
    			DataLine.Info info2 = new DataLine.Info(Clip.class, sound2.getFormat());
    		    snare = (Clip) AudioSystem.getLine(info2);
    		    snare.open(sound);
     
    		}
    		catch (Exception exc)
            {
                exc.printStackTrace();
            }
     
    	}
     
    public void makeNoise(String s){
     
    try{
    	if(s.equalsIgnoreCase("Ethernet (IPv4 (TCP))")) 
    		{							
    			kick.loop(1);
    		}
     
    	else if(s.equalsIgnoreCase("Ethernet (IPv4 (UDP ()))"))
    		{
    			snare.loop(1);
    		}
    catch (Exception exc)
    	        {
    	            exc.printStackTrace();
    	        }
    	}
     
     
    }
     
     
     
     
    ...much other code ommitted...


    I think I do not probably need to have two everything? Is this right? Please and thank you for response!


  2. #2
    Junior Member
    Join Date
    Apr 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with java audio samples please!

    Please post the error message if you are getting any:

    Also please post Registry, makeFrame and initSynth, classes/methods that you have created.
    Last edited by refhat; April 19th, 2010 at 08:45 AM.

Similar Threads

  1. Keep it simple for the retard- Outputting audio on a trigger.
    By andypooz in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 17th, 2010, 08:14 AM
  2. sun.audio
    By bguy in forum Java SE APIs
    Replies: 3
    Last Post: November 12th, 2009, 01:17 AM
  3. How to detect audio input from a telephone call?
    By ces_31 in forum Java Theory & Questions
    Replies: 0
    Last Post: August 12th, 2009, 12:45 AM

Tags for this Thread