I wrote Snake code, and want to add a sound effect when snake eats an apple. I copyied a code from some guy on YT, but it doesn't work to me. Can somebody explain me how to do this?
Code:
import com.sun.tools.javac.Main;
import javax.sound.sampled.*;
import java.io.IOException;
import java.net.URL;
public class AppleEatSoundEffect {
public static Mixer mixer;
public static Clip clip;
public static void main(String[] args) {
Mixer.Info[] mixInfos = AudioSystem.getMixerInfo();
mixer = AudioSystem.getMixer(mixInfos[0]);
DataLine.Info dataInfo = new DataLine.Info(Clip.class, null);
try {
clip = (Clip) mixer.getLine(dataInfo);
} catch (LineUnavailableException lue) {
lue.printStackTrace();
}
try {
URL soundURL = Main.class.getResource("NotBad.wav");
AudioInputStream audioStream = AudioSystem.getAudioInputStream(soundURL);
clip.open(audioStream);
} catch (LineUnavailableException lue) {
lue.printStackTrace();
} catch (UnsupportedAudioFileException uafe) {
uafe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
clip.start();
do {
try {
Thread.sleep(100);
} catch (InterruptedException ie) {
ie.printStackTrace();
}
} while (clip.isActive());
}
}
Compiler says thas something wrong with clip = (Clip) mixer.getLine(dataInfo);
:
Exception in thread "main" java.lang.IllegalArgumentException: Line unsupported: interface Clip at java.desktop/com.sun.media.sound.PortMixer.getLine(PortMixer.java:131)