如何解决我在 2 个不同的文件中有 2 个声音,如果第一个声音结束,我想播放一个声音
我有两个声音,我想在按下按钮后播放第一首歌曲,并在声音结束后播放第二个声音。
所以如果第一个声音结束播放第二个声音,我想使用 if 语句。
import javax.sound.sampled.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
public class my_Frame extends JFrame implements ActionListener {
JButton button;
JButton button2;
JButton button3;
JButton button4;
JLabel label;
Clip clip;
Clip clip2;
my_Frame() throws UnsupportedAudioFileException,IOException,LineUnavailableException {
File file = new File("src/GUI/Buttons/Quiz/music.wav");
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(file);
clip = AudioSystem.getClip();
clip.open(audioInputStream);
File file2 = new File("src/GUI/Buttons/Quiz/READYSETGO_Trim.wav");
AudioInputStream audioInputStream2 = AudioSystem.getAudioInputStream(file2);
clip2 = AudioSystem.getClip();
clip2.open(audioInputStream2);
ImageIcon answer_label= new ImageIcon("src/GUI/Buttons/Quiz/Answer_Laber.png");
ImageIcon imageIcon = new ImageIcon("src/GUI/Buttons/Quiz/Text_Label.png");
label = new JLabel();
label.setBounds(25,50,700,200);
label.setText("<html>This is the ultimate quiz <br>do you want try it ?</html>");
label.setHorizontalTextPosition(JLabel.CENTER);
label.setFont(new Font("",Font.PLAIN,25));
label.setIcon(imageIcon);
button = new JButton();
button.addActionListener(this);
button.setText("yes");
button.setBounds(200,300,100,100);
button.setHorizontalTextPosition(JButton.CENTER);
button.setFocusable(false);
button.setVisible(true);
button.setIcon(answer_label);
button2 = new JButton();
button2.addActionListener(this);
button2.setText("no");
button2.setBounds(350,100);
button2.setHorizontalTextPosition(JButton.CENTER);
button2.setFocusable(false);
button2.setVisible(true);
button2.setIcon(answer_label);
button3 = new JButton();
button3.addActionListener(this);
button3.setText("2");
button3.setBounds(200,100);
button3.setHorizontalTextPosition(JButton.CENTER);
button3.setFocusable(false);
button3.setIcon(answer_label);
button4 = new JButton();
button4.addActionListener(this);
button4.setText("4");
button4.setBounds(350,100);
button4.setHorizontalTextPosition(JButton.CENTER);
button4.setFocusable(false);
button4.setIcon(answer_label);
this.setDefaultCloSEOperation(EXIT_ON_CLOSE);
this.setSize(760,600);
this.setVisible(true);
this.setLocation(790,0);
this.add(button);
this.add(button2);
this.add(label);
this.getContentPane().setBackground(Color.BLACK);
this.setLayout(null);
}
这是按钮功能。我想如果我按下按钮 clip2
开始,在它结束后第一个剪辑开始。
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button) {
JOptionPane.showMessageDialog(null,"niiiiiiiiiice");
clip2.start();
getContentPane().removeAll();
repaint();
label.setText("What is 1+1");
this.add(button3);
this.add(button4);
this.add(label);
}
if(e.getSource()==button2){
System.exit(0);
}
}
}
解决方法
Clip
实现了 Line
,因此您可以使用 LineListener 以便在 Clip
结束时获得通知(LineEvent.Type = STOP)。
实现监听的类可以设置为在它收到通知时触发您想要发生的下一件事。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。