Adding Music to My Soundcard Project
So I am building a sound board for my CS project using java. The sound card consists of an 8-piece drum set that is supposed to play its set sound when pressed. I have set the area to be clicked, but I don’t know how to implement a sound that will start when the specified area is clicked.
// FinalProjectst.java
// AP Computer ScienceStudent Version
import java.awt.*;
import java.applet.*;
import java.awt.geom.Ellipse2D;
public class FinalProjectst extends Applet
{
Image picture;
Ellipse2D base, bT, snare, lT, rT, hh, lC, rC;
int numColor;
public void init()
{
picture = getImage(getDocumentBase(),"drumSet.jpg");
base = new Ellipse2D.Double (355, 415, 305, 240); //Bass
bT = new Ellipse2D.Double (715, 360, 325, 245); //Bottom Tom
snare = new Ellipse2D.Double ( 35, 410, 290, 200); //Snare
lT = new Ellipse2D.Double (283, 130, 185, 165); //Left Tom
rT = new Ellipse2D.Double (543, 120, 200, 175); //Right Tom
hh = new Ellipse2D.Double ( 0, 225, 250, 150); //High Hat
lC = new Ellipse2D.Double ( 10, 0, 305, 195); //Left Cymbal
rC = new Ellipse2D.Double (765, 0, 505, 275); //Right Cymbal
}
public boolean contains(Event e, int x, int y)
{
if(base.contains(x,y))
numColor = 1;
else if(bT.contains(x,y))
numColor = 2;
else if(snare.contains(x,y))
numColor = 3;
else if(lT.contains(x,y))
numColor = 4;
else if(rT.contains(x,y))
numColor = 5;
else if(hh.contains(x,y))
numColor = 6;
else if(lC.contains(x,y))
numColor = 7;
else if(rC.contains(x,y))
numColor = 8;
else
numColor = 9;
repaint();
return true;
}
public void paint(Graphics g)
{
g.drawImage(picture, 0, 0, this);
}
}
+3
source to share
1 answer
There are many ways to play sound, but for example you can do something like this:
public static void playSound(File soundfile) throws LineUnavailableException, UnsupportedAudioFileException, IOException{
AudioInputStream audioInputStream = null;
audioInputStream = AudioSystem.getAudioInputStream(soundfile);
Clip clip = AudioSystem.getClip();
clip.open(audioInputStream);
clip.start();
}
This code will play wav files without issue, and I think it will play other types of audio files as well, but I'm not sure which audio types it will play and what types of audio it won't.
I hope this help :)
EDIT:
As you can see, there are many exceptions that can be thrown from this code, so you probably want to handle them accordingly.
+2
source to share