如何在播放新片段之前停止 Java 中所有当前正在播放的片段?
How to stop all currently playing clips in Java before playing a new one?
我正在使用 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();
}
我正在使用 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();
}