gpt4 book ai didi

java - 如何在播放新剪辑之前停止 Java 中当前正在播放的所有剪辑?

转载 作者:行者123 更新时间:2023-12-01 11:36:42 25 4
gpt4 key购买 nike

我正在使用 Swing 为我的大学项目制作一个智能 ATM 应用程序,该应用程序也可供视力障碍人士使用。

我在每个按钮上添加了鼠标悬停监听器,以便程序播放一个讲述该按钮操作的剪辑。

问题是,当用户在短时间内将鼠标悬停在多个按钮上时,当有关上一个按钮的剪辑已经播放时,两个剪辑会同时播放,这不是我想要的。

到目前为止,我的当前帧代码是:

import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.util.InputMismatchException;

import javax.imageio.ImageIO;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.*;



public class InitialInput extends JFrame implements ActionListener {
JLabel logo;
JPanel logoPanel = new JPanel();

JPanel askPanel = new JPanel();
JPanel pinPanel = new JPanel();

//For adding span
JPanel emptyPanel = new JPanel();

JTextField accountNumberField = new JTextField(10);
JLabel askAccountNumber = new JLabel("Please enter your account Number : ");
public int userAccountNumber;

JTextField pinField = new JTextField(10);
JLabel askPin = new JLabel("Please enter your Pin : ");
public int userPin;

JPanel submitPanel = new JPanel();
JButton submit = new JButton("SUBMIT");
JButton createAccount = new JButton("CREATE NEW ACCOUNT");
JPanel mainPanel = new JPanel(new GridBagLayout());
Clip clipAccountNumberField,clipPinField,clipWelcome,clipSubmit;
InitialInput() {
//Play welcome Music
try {
AudioInputStream audioIn = AudioSystem.getAudioInputStream(Welcome.class.getResource("WelcomeMusic.wav"));
clipWelcome = AudioSystem.getClip();
clipWelcome.open(audioIn);
clipWelcome.start();
}
catch(Exception e) {
System.out.println("Sorry Unable to play deposit music!");
}
buildGUI();


}
//TODO Work on this
/*
public void stopAllClips() {
//Clip clipAccountNumberField,clipPinField,clipWelcome,clipSubmit;
if(clipWelcome.isActive())
clipWelcome.stop();
if(clipAccountNumberField.isActive())
clipAccountNumberField.stop();
if(clipPinField.isActive())
clipPinField.stop();
if(clipSubmit.isActive())
clipSubmit.stop();
}*/
public void buildGUI() {
//Properties of Frame
setSize(400,300);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

try {

BufferedImage logoI = ImageIO.read(this.getClass().getResource("logo.png"));
logo = new JLabel(new ImageIcon(logoI));
}
catch(Exception e) {
e.getMessage();

}
//On Hover Action for Deposit Button
accountNumberField.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseEntered(java.awt.event.MouseEvent evt) {
try {
AudioInputStream audioIn = AudioSystem.getAudioInputStream(InitialInput.class.getResource("ClickAndEnterAccountNumber.wav"));
clipAccountNumberField = AudioSystem.getClip();
clipAccountNumberField.open(audioIn);
// stopAllClips();
clipAccountNumberField.start();
}
catch(Exception e) {
System.out.println("Sorry Unable to play account number music!");
}

}
});
pinField.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseEntered(java.awt.event.MouseEvent evt) {
try {
AudioInputStream audioIn = AudioSystem.getAudioInputStream(InitialInput.class.getResource("ClickAndEnterPin.wav"));
clipPinField = AudioSystem.getClip();
clipPinField.open(audioIn);
// stopAllClips();
clipPinField.start();
}
catch(Exception e) {
System.out.println("Sorry Unable to play pin music!");
}

}
});
submit.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseEntered(java.awt.event.MouseEvent evt) {
try {
AudioInputStream audioIn = AudioSystem.getAudioInputStream(InitialInput.class.getResource("ClickToSubmit.wav"));
clipSubmit = AudioSystem.getClip();
clipSubmit.open(audioIn);
// stopAllClips();
clipSubmit.start();
}
catch(Exception e) {
System.out.println("Sorry Unable to play submit music!");
}

}
});
//TODO Add music to create account button
//Adds gap between create account and submit button
FlowLayout flow = new FlowLayout(); // Create a layout manager
flow.setHgap(35);
submitPanel.setLayout(flow);

//Adds symbiosis logo to panel
logoPanel.add(logo);

//Adds action listener to text fields
accountNumberField.addActionListener(this);
pinField.addActionListener(this);
//Adds action listener to Create Account Button
createAccount.addActionListener(this);
//Adds labels to panel
askPanel.add(askAccountNumber);
askPanel.add(accountNumberField);

//Adds textfields to panel
pinPanel.add(askPin);
pinPanel.add(pinField);

//Adds buttons to panels
submitPanel.add(createAccount);
submitPanel.add(submit);

//Specifies constraints for grid layout
GridBagConstraints c = new GridBagConstraints();
c.gridx=0;
c.gridy=0;
mainPanel.add(logoPanel,c);

c.gridx=0;
c.gridy=1;
mainPanel.add(askPanel,c);

c.gridx=0;
c.gridy=2;
mainPanel.add(emptyPanel,c);

c.gridx=0;
c.gridy=3;
mainPanel.add(pinPanel,c);

c.gridx=0;
c.gridy=4;
mainPanel.add(emptyPanel,c);

c.gridx=0;
c.gridy=5;
mainPanel.add(submitPanel,c);

add(mainPanel);
setVisible(true);
}
//Action listener
public void actionPerformed(ActionEvent e) {
if(e.getSource()==accountNumberField) {
try {
userAccountNumber = Integer.parseInt(accountNumberField.getText());
int numberOfDigitsInAccountNumber = String.valueOf(userAccountNumber).length();
if(numberOfDigitsInAccountNumber !=6) {
throw new InputMismatchException();
}
if(userAccountNumber <= 0) {
throw new Exception();
}
//TODO if(accountNumber) not present in database
}
catch(InputMismatchException e1) {
accountNumberField.setText("");
JOptionPane.showMessageDialog(accountNumberField,"Please enter a 6 digit account number.");
}
catch(Exception e1) {
accountNumberField.setText("");
JOptionPane.showMessageDialog(accountNumberField,"Invalid input.");
}
}
else if(e.getSource()==pinField) {
try {
userPin = Integer.parseInt(pinField.getText());
int numberOfDigitsInAccountNumber = String.valueOf(userPin).length();
if(numberOfDigitsInAccountNumber !=4) {
throw new InputMismatchException();
}
if(userPin <= 0) {
throw new Exception();
}
//TODO if(accountNumber) not match in database
}
catch(InputMismatchException e1) {
pinField.setText("");
JOptionPane.showMessageDialog(pinField,"Please enter a 4 digit pin.");
}
catch(Exception e1) {
pinField.setText("");
JOptionPane.showMessageDialog(pinField,"Invalid input.");
}
}
else if(e.getSource()==createAccount) {
CreateAccount a = new CreateAccount();
this.setVisible(false);
a.setVisible(true);
}
}
}

最佳答案

重写每个MouseListener的mouseExited方法,在实现中停止Clip

@Override
public void mouseExited(MouseEvent e){
theClipIManage.stop();
}

关于java - 如何在播放新剪辑之前停止 Java 中当前正在播放的所有剪辑?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29903194/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com