当前位置: 代码迷 >> java >> 我需要知道在用户设置时间后如何调用警报声音
  详细解决方案

我需要知道在用户设置时间后如何调用警报声音

热度:30   发布时间:2023-07-27 09:19:35.0

我已经尝试了2-3天,以找到一种方法来调用play()方法,这是我的闹钟声音,但没有真正的成功...我尝试过并在循环中使用带有布尔标志的新TimerTasks并没有成功。 ..警报声音在遇到用户输入时不会触发,因此我可以请经验丰富的人告诉我我做错了什么吗?

这是我的Commands类,其中放置了警报声音,该声音本身连接在主类ClockGui中

 package Clock;

 import java.io.*;
 import java.text.DecimalFormat;
  import java.util.Calendar;
  import java.util.Timer;
 import java.util.TimerTask;

import javax.swing.*;
import sun.audio.*;

public class Commands {
ClockGui gui;
Time clock;
Timezones zone;
 DecimalFormat df = new DecimalFormat("00");
int hour1 ;
int min1 ;
int sec1;

boolean alarmOn = false;


public Commands (ClockGui gui) {
     this.gui = gui;
     zone = new Timezones(gui);
     clock = new Time(gui);

}

public void setAlarm() {
      hour1 = Integer.parseInt(JOptionPane.showInputDialog("Set the hour of the alarm \n in the form of '00' :"));
      min1 = Integer.parseInt(JOptionPane.showInputDialog("Set the minutes of the alarm \n in the form of '00' :"));
      sec1 = Integer.parseInt(JOptionPane.showInputDialog("Set the seconds of the alarm \n in the form of '00' :"));
      gui.alarm1.setText("" + df.format(hour1) + " : " + df.format(min1) + " : " + df.format(sec1));
      Timer timer2 = new Timer();
       TimerTask task2 = new TimerTask() {
           public void run(){
               Calendar timeNow = Calendar.getInstance();
               int hours = timeNow.get(Calendar.HOUR_OF_DAY);
               int mins = timeNow.get(Calendar.MINUTE);

               if (hours == hour1 && mins == min1) {
                   alarmOn = true;
               }

           }
       };
       timer2.scheduleAtFixedRate(task2,0,1000);



 }

public void play() {
if(alarmOn == true){

    InputStream sound;
    try {
        sound = new FileInputStream(new File("C:\\Users\\User\\workspace\\Alarm Clock\\src\\Clock\\sound.wav"));
        AudioStream alarm = new AudioStream(sound);
        AudioPlayer.player.start(alarm);
    } catch (Exception e){
        JOptionPane.showMessageDialog(null, e);
    }
}

}

这是我的主班

package Clock;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.*;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;

 import javax.imageio.ImageIO;
import javax.print.DocFlavor.URL;
import javax.swing.*;
import javax.swing.event.MenuEvent;
 import javax.swing.event.MenuListener;

 public class ClockGui implements ActionListener, MenuListener,   MouseListener {
  Time clock;
  Commands command;
  Timezones zone;
  JFrame frame = new JFrame("Clock");
  JLabel label1 = new JLabel("The Time is now :");
  JLabel label2 = new JLabel("Stopwatch");
  JLabel label3 = new JLabel("Select a country :");
  JPanel panel1 = new JPanel();
  JPanel panel3 = new JPanel();
  JPanel panel2 = new JPanel();
  JPanel panel4 = new JPanel();
  JPanel panel5 = new JPanel();
  JPanel panel6 = new JPanel();
  JButton start = new JButton("Start");
  JButton stop = new JButton("Stop");
  Container contentPane = new Container();
  JTextField time = new JTextField(10);
  JTextField stopwatch = new JTextField(20);
  JTextField timeZone = new JTextField(6);
  JTextField alarm1 = new JTextField(7);
  JButton alarm = new JButton("Set Alarm");
  JButton reset = new JButton("Reset");


public ClockGui() throws IOException   {
    clock = new Time(this);
    command = new Commands(this);
    zone = new Timezones(this);
    makeFrame();
    if (command.alarmOn == true){
        command.play();
    }


}

public static void main(String[] args) throws IOException   {

    new ClockGui();



}

只需删除alarmOn变量并调用play()而不是在TimerTask中设置alarmOn = true

  相关解决方案