我对我的代码有疑问,首先是关于它的内容的一些预先信息......
我的平面设计的朋友,她的大学有一个展览/项目用声学药物影响人们(展览的主题是毒品......)
她希望有一个界面,用户可以按下按钮听一种声学药物。此外,用户应该有机会,混合像kokain和海洛因这样的声音,并通过每次按下按钮或只停止一个按钮来停止所有按钮。
我的代码有问题。在我调试代码时,我的第二个监听按钮没有显示,你必须按下按钮可见的窗口的左侧机器人,你可以启动一个标题并停止它,但如果你在同一个播放两个声音时间,你可以阻止一个,如果你停止第二个,它表明它停止了但实际上没有。
如果有人可以帮助我,我会很高兴。
这里是代码:
package test;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineEvent;
import javax.sound.sampled.LineListener;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.JButton;
import javax.swing.JFrame;
public class c extends JFrame {
String lsd="/Users/shaggy/Desktop/Lsd.wav";
JButton btnPlaySoundLSD=new JButton("Listen");
Clip clip = null;
String koka ="/Users/shaggy/Desktop/Kokain.wav";
JButton btnPlaySoundKokain=new JButton("Listen");
public c(){
btnPlaySoundLSD.setBounds(0, 0, 250, 240);
btnPlaySoundLSD.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
JButton b=(JButton)arg0.getSource();
// play the sound clip
if(b.getText().equals("Listen")){
b.setText("Stop");
btnPlaySoundCLickLSD();
} else if(btnPlaySoundLSD.getText().equals("Stop")) {
b.setText("Listen");
clip.stop();
}
} catch (LineUnavailableException | IOException
| UnsupportedAudioFileException e) {
e.printStackTrace();
}
}
});
setSize(500, 500);
getContentPane().setLayout(null);
getContentPane().add(btnPlaySoundLSD);
setVisible(true);
btnPlaySoundKokain.setBounds(0, 238, 250, 240);
btnPlaySoundKokain.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg1) {
try {
JButton b=(JButton)arg1.getSource();
// play the sound clip
if(b.getText().equals("Listen")){
b.setText("Stop");
btnPlaySoundCLickKokain();
} else if(btnPlaySoundKokain.getText().equals("Stop")) {
b.setText("Listen");
clip.stop();
}
} catch (LineUnavailableException | IOException
| UnsupportedAudioFileException e) {
e.printStackTrace();
}
}
});
setSize(500, 500);
getContentPane().setLayout(null);
getContentPane().add(btnPlaySoundKokain);
setVisible(true);
}
private void btnPlaySoundCLickKokain() throws LineUnavailableException, IOException, UnsupportedAudioFileException{
File soundFile = new File(koka);
AudioInputStream sound = AudioSystem.getAudioInputStream(soundFile);
DataLine.Info info = new DataLine.Info(Clip.class, sound.getFormat());
clip = (Clip) AudioSystem.getLine(info);
clip.open(sound);
clip.addLineListener(new LineListener() {
public void update(LineEvent event) {
if (event.getType() == LineEvent.Type.STOP) {
System.out.println("stop");
event.getLine().close();
}
}
});
clip.start();
}
private void btnPlaySoundCLickLSD() throws LineUnavailableException, IOException, UnsupportedAudioFileException{
File soundFile = new File(lsd);
AudioInputStream sound = AudioSystem.getAudioInputStream(soundFile);
DataLine.Info info = new DataLine.Info(Clip.class, sound.getFormat());
clip = (Clip) AudioSystem.getLine(info);
clip.open(sound);
clip.addLineListener(new LineListener() {
public void update(LineEvent event) {
if (event.getType() == LineEvent.Type.STOP) {
System.out.println("stop");
event.getLine().close();
}
}
});
clip.start();
}
public static void main(String[] args) {
c cc=new c();
}
}