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

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

我有两种声音,我想在按下按钮后播放第一首歌,在声音结束后,我想播放第二种声音。因此,如果第一个声音结束播放第二个声音,我想使用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, 300, 100, 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, 300, 100, 100);
        button3.setHorizontalTextPosition(JButton.CENTER);
        button3.setFocusable(false);
        button3.setIcon(answer_label);

        button4 = new JButton();
        button4.addActionListener(this);
        button4.setText("4");
        button4.setBounds(350, 300, 100, 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);

    }

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

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);
        }

    }
}
icomxhvb

icomxhvb1#

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

相关问题