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!