I know about the clip.stop() method, however it doesn't seem to work within when I have it inside the key_events. It just causes an Error. Well I know why it causes the error. Because I'm asking it to essentially stop a clip that doesn't exist until a few lines later. But using this same logic or close to it if possible, how could I recode that so it knows to select the previous clip that was playing from a previous key_event. The functionality I'm intending for is: When F1 is pressed, a wav plays. When F2 is pressed, current wav STOPS, new wav STARTS. When F3 is pressed, current wav STOPS, new wav STARTS. Etc etc etc.
case KeyEvent.VK_F1:
try {
//stop any sound
clip.stop();
sample = AudioSystem.getAudioInputStream(getURL(filename));
//create a sound buffer
Clip clip = AudioSystem.getClip();
//load the audio file
clip.open(sample);
//play sample
clip.start();
} catch (MalformedURLException ez) {
} catch (IOException ez) {
} catch (LineUnavailableException ez) {
} catch (UnsupportedAudioFileException ez) {
} catch (Exception ez) { }
break;
case KeyEvent.VK_F2:
try {
//stop any sound
clip.stop();
sample = AudioSystem.getAudioInputStream(getURL(filename2));
//create a sound buffer
Clip clip = AudioSystem.getClip();
//load the audio file
clip.open(sample);
//play sample
clip.start();
} catch (MalformedURLException ez) {
} catch (IOException ez) {
} catch (LineUnavailableException ez) {
} catch (UnsupportedAudioFileException ez) {
} catch (Exception ez) { }
break;
case KeyEvent.VK_F3:
try {
//stop any sound
clip.stop();
sample = AudioSystem.getAudioInputStream(getURL(filename3));
//create a sound buffer
Clip clip = AudioSystem.getClip();
//load the audio file
clip.open(sample);
//play sample
clip.start();
} catch (MalformedURLException ez) {
} catch (IOException ez) {
} catch (LineUnavailableException ez) {
} catch (UnsupportedAudioFileException ez) {
} catch (Exception ez) { }
break;
case KeyEvent.VK_F4:
try {
//stop any sound
clip.stop();
sample = AudioSystem.getAudioInputStream(getURL(filename4));
//create a sound buffer
Clip clip = AudioSystem.getClip();
//load the audio file
clip.open(sample);
//play sample
clip.start();
} catch (MalformedURLException ez) {
} catch (IOException ez) {
} catch (LineUnavailableException ez) {
} catch (UnsupportedAudioFileException ez) {
} catch (Exception ez) { }
break;
case KeyEvent.VK_F5:
try {
//stop any sound
clip.stop();
sample = AudioSystem.getAudioInputStream(getURL(filename5));
//create a sound buffer
Clip clip = AudioSystem.getClip();
//load the audio file
clip.open(sample);
//play sample
clip.start();
} catch (MalformedURLException ez) {
} catch (IOException ez) {
} catch (LineUnavailableException ez) {
} catch (UnsupportedAudioFileException ez) {
} catch (Exception ez) { }
break;
case KeyEvent.VK_F6:
try {
//stop any sound
clip.stop();
sample = AudioSystem.getAudioInputStream(getURL(filename6));
//create a sound buffer
Clip clip = AudioSystem.getClip();
//load the audio file
clip.open(sample);
//play sample
clip.start();
} catch (MalformedURLException ez) {
} catch (IOException ez) {
} catch (LineUnavailableException ez) {
} catch (UnsupportedAudioFileException ez) {
} catch (Exception ez) { }
break;
case KeyEvent.VK_F7:
try {
//stop any sound
clip.stop();
sample = AudioSystem.getAudioInputStream(getURL(filename7));
//create a sound buffer
Clip clip = AudioSystem.getClip();
//load the audio file
clip.open(sample);
//play sample
clip.start();
} catch (MalformedURLException ez) {
} catch (IOException ez) {
} catch (LineUnavailableException ez) {
} catch (UnsupportedAudioFileException ez) {
} catch (Exception ez) { }
break;
Any help would be greatly appreciated :) Thanks