Java 触发第二个wav文件时暂停第一个播放wav文件。如何?
我这里有一个代码,它监听用户的输入Java 触发第二个wav文件时暂停第一个播放wav文件。如何?,java,audio,javasound,Java,Audio,Javasound,我这里有一个代码,它监听用户的输入 System.out.println(message);{ if(message.equals("play")){ try { AudioInputStream inputStream = AudioSystem.getAudioInputStream(new File("c:/DancingMachupapet/Test.wav"));
System.out.println(message);{
if(message.equals("play")){
try {
AudioInputStream inputStream = AudioSystem.getAudioInputStream(new File("c:/DancingMachupapet/Test.wav"));
Clip clip = AudioSystem.getClip();
// getAudioInputStream() also accepts a File or InputStream
clip.open(inputStream);
clip.start();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JOptionPane.showMessageDialog(null, "Close to exit!");
}
});
}catch (IOException e) {
}
}
if(message.equals("hoy")){
try {
AudioInputStream inputStream = AudioSystem.getAudioInputStream(new File("c:/DancingMachupapet/ThinkOfManu.wav"));
Clip clip = AudioSystem.getClip();
// getAudioInputStream() also accepts a File or InputStream
clip.open(inputStream);
clip.start();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JOptionPane.showMessageDialog(null, "Close to exit!");
}
});
}catch (IOException e) {
}
}
}
此代码在看到消息“hoy”和“play”时工作,它将分别播放“ThinkOfManu.wav”和“Test.wav”。它可以工作,但我的问题是,当我同时输入'hoy'和'play'时,两个wav文件也会同时播放
我想发生的是,当ThinkOfManu.wav播放时,我调用Test.wav,ThinkOfManu应该停止播放Test.wav。不是同时
请在此帮助我。我假设Clip.stop方法确实存在 每次运行新歌时,都会创建一个新剪辑对象。 不要这样做,而是在触发之前在try-catch块外部初始化clip对象 clip.play(),执行clip.stop 那应该对你有用 示例代码:
Clip clip = AudioSystem.getClip();
if(message.equals("play"))
{
try {
AudioInputStream inputStream = AudioSystem.getAudioInputStream(new File("c:/DancingMachupapet/Test.wav"));
clip.stop();
clip.open(inputStream);
clip.start();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JOptionPane.showMessageDialog(null, "Close to exit!");
}});
}catch (IOException e) {
}
}
if(message.equals("hoy")){
try {
AudioInputStream inputStream = AudioSystem.getAudioInputStream(new File("c:/DancingMachupapet/ThinkOfManu.wav"));
clip.open(inputStream);
clip.start();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JOptionPane.showMessageDialog(null, "Close to exit!");
}
});
}catch (IOException e) {
}
}
}
将clip.stop()放置在何处;?我移除了我的try-catch块,但它仍然同时播放@neerajal,如果我是你,我不会使用这种方法。使用“command”:“filepath”的哈希映射,并创建一个函数,该函数接受该命令,并播放相关文件,从映射中获取值。的可能重复。因为你显然忽视了我的建议,所以这也带来了-1。