我在两个不同的文件中有两个声音,如果第一个声音结束,我想播放一个声音

tjrkku2a  于 2021-08-20  发布在  Java
关注(0)|答案(1)|浏览(450)

我有两种声音,我想在按下按钮后播放第一首歌,在声音结束后,我想播放第二种声音。因此,如果第一个声音结束播放第二个声音,我想使用if语句。

  1. import javax.sound.sampled.*;
  2. import javax.swing.*;
  3. import java.awt.*;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.io.File;
  7. import java.io.IOException;
  8. public class my_Frame extends JFrame implements ActionListener {
  9. JButton button;
  10. JButton button2;
  11. JButton button3;
  12. JButton button4;
  13. JLabel label;
  14. Clip clip;
  15. Clip clip2;
  16. my_Frame() throws UnsupportedAudioFileException, IOException, LineUnavailableException {
  17. File file = new File("src/GUI/Buttons/Quiz/music.wav");
  18. AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(file);
  19. clip = AudioSystem.getClip();
  20. clip.open(audioInputStream);
  21. File file2 = new File("src/GUI/Buttons/Quiz/READYSETGO_Trim.wav");
  22. AudioInputStream audioInputStream2 = AudioSystem.getAudioInputStream(file2);
  23. clip2 = AudioSystem.getClip();
  24. clip2.open(audioInputStream2);
  25. ImageIcon answer_label= new ImageIcon("src/GUI/Buttons/Quiz/Answer_Laber.png");
  26. ImageIcon imageIcon = new ImageIcon("src/GUI/Buttons/Quiz/Text_Label.png");
  27. label = new JLabel();
  28. label.setBounds(25, 50, 700, 200);
  29. label.setText("<html>This is the ultimate quiz <br>do you want try it ?</html>");
  30. label.setHorizontalTextPosition(JLabel.CENTER);
  31. label.setFont(new Font("", Font.PLAIN, 25));
  32. label.setIcon(imageIcon);
  33. button = new JButton();
  34. button.addActionListener(this);
  35. button.setText("yes");
  36. button.setBounds(200, 300, 100, 100);
  37. button.setHorizontalTextPosition(JButton.CENTER);
  38. button.setFocusable(false);
  39. button.setVisible(true);
  40. button.setIcon(answer_label);
  41. button2 = new JButton();
  42. button2.addActionListener(this);
  43. button2.setText("no");
  44. button2.setBounds(350, 300, 100, 100);
  45. button2.setHorizontalTextPosition(JButton.CENTER);
  46. button2.setFocusable(false);
  47. button2.setVisible(true);
  48. button2.setIcon(answer_label);
  49. button3 = new JButton();
  50. button3.addActionListener(this);
  51. button3.setText("2");
  52. button3.setBounds(200, 300, 100, 100);
  53. button3.setHorizontalTextPosition(JButton.CENTER);
  54. button3.setFocusable(false);
  55. button3.setIcon(answer_label);
  56. button4 = new JButton();
  57. button4.addActionListener(this);
  58. button4.setText("4");
  59. button4.setBounds(350, 300, 100, 100);
  60. button4.setHorizontalTextPosition(JButton.CENTER);
  61. button4.setFocusable(false);
  62. button4.setIcon(answer_label);
  63. this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  64. this.setSize(760, 600);
  65. this.setVisible(true);
  66. this.setLocation(790, 0);
  67. this.add(button);
  68. this.add(button2);
  69. this.add(label);
  70. this.getContentPane().setBackground(Color.BLACK);
  71. this.setLayout(null);
  72. }

这是按钮功能,我想如果我按下按钮,剪辑2开始,结束后,第一个剪辑开始

  1. public void actionPerformed(ActionEvent e) {
  2. if (e.getSource() == button) {
  3. JOptionPane.showMessageDialog(null, "niiiiiiiiiice");
  4. clip2.start();
  5. getContentPane().removeAll();
  6. repaint();
  7. label.setText("What is 1+1");
  8. this.add(button3);
  9. this.add(button4);
  10. this.add(label);
  11. }
  12. if(e.getSource()==button2){
  13. System.exit(0);
  14. }
  15. }
  16. }
icomxhvb

icomxhvb1#

Clip 工具 Line ,因此可以使用linelistener在 Clip 结束(lineevent.type=stop)。
可以将实现侦听的类设置为在接收到通知时触发您希望发生的下一件事情。

相关问题