我知道clip.stop()方法,但是当我在key_events中使用它时它似乎不起作用。它只会导致错误。我知道它为什么会导致错误。因为我要求它基本上停止一个不存在的剪辑,直到几行之后。但是如果可能的话,使用相同的逻辑或接近它,我怎么能重新编码,以便它知道选择从前一个key_event播放的前一个剪辑。我想要的功能是:当按下F1时,wav会播放。当按下F2时,当前wav STOPS,新的wav STARTS。当按下F3时,当前wav STOPS,新的wav STARTS。等等等。
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;
任何帮助将不胜感激:)谢谢
答案 0 :(得分:1)
很难100%肯定,但看起来你正在影响你的变量......
clip.stop();
sample = AudioSystem.getAudioInputStream(getURL(filename));
//create a sound buffer
Clip clip = AudioSystem.getClip();
事实上,我甚至不确定这会如何编译......
将Clip
定义为实例变量,初始化程序时,同时初始化Clip
。
您应该可以随时拨打stop
,但这只会将Clip
重置回当前输入的开头。你需要做的也是关闭Clip
,它释放了Clip
为当前输入管理的内部资源......
KeyListener
也是众所周知的麻烦,您应该考虑使用Key Bindings,因为它们为您提供控制以确定可以生成关键事件的焦点水平,例如...... < / p>
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TestPlayer {
public static void main(String[] args) {
new TestPlayer();
}
public TestPlayer() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
try {
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
} catch (LineUnavailableException exp) {
exp.printStackTrace();
}
}
});
}
public class TestPane extends JPanel {
private Clip clip;
private List<File> playList;
private int index;
private JLabel label;
public TestPane() throws LineUnavailableException {
label = new JLabel("Play stuff");
add(label);
clip = AudioSystem.getClip();
File[] files = new File("A folder of music files").listFiles(new FileFilter() {
@Override
public boolean accept(File file) {
return file.getName().toLowerCase().endsWith(".wav");
}
});
playList = new ArrayList<>(Arrays.asList(files));
index = -1;
InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW);
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), "previous");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), "next");
ActionMap am = getActionMap();
am.put("next", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
playNext();
}
});
am.put("previous", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
playPrevious();
}
});
}
public void playNext() {
index++;
if (index >= playList.size()) {
index = 0;
}
File file = playList.get(index);
play(file);
}
public void playPrevious() {
index--;
if (index < 0) {
index = playList.size() - 1;
}
File file = playList.get(index);
play(file);
}
public void play(File file) {
try {
stop();
label.setText(file.getName());
AudioInputStream sample = AudioSystem.getAudioInputStream(file);
clip.open(sample);
clip.start();
} catch (UnsupportedAudioFileException | IOException | LineUnavailableException exp) {
exp.printStackTrace();
}
}
public void stop() {
clip.stop();
clip.close();
}
}
}