【问题标题】:Old Fashioned Java countdown alarm老式 Java 倒计时警报
【发布时间】:2013-12-11 17:23:11
【问题描述】:

新年前夜快到了,我在想有哪些可能的方法可以将老式倒数计时器编程到 Java 中以将其用作 10 秒的倒计时。

我所说的老式倒计时是http://www.youtube.com/watch?v=9h7J5VWUOYw

我能想到的只是使用 JLabel 使用 Timer 做一个简单的倒计时。

编辑:我所做的是制作一个时钟全屏并使用 VLCJ 在 23:59:30 播放一个简单的倒计时视频

public class TimerFrame extends JFrame{

    SimpleDateFormat dateFormatter = new SimpleDateFormat(" H:mm:ss");
    Date now = new Date();
    private JLabel lblTime = new JLabel();
    private final EmbeddedMediaPlayerComponent mediaPlayerComponent;
    private String countdownVideo ="C:\\Users\\LT\\Desktop\\newyear\\cd2.mp4";
    private String vlcFolder = "C:/Program Files/VideoLAN/VLC";
    private java.util.Timer timerVideoRun = new java.util.Timer();
    private Timer timer;

public TimerFrame()
{
    //time for video to run on
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 23);
    calendar.set(Calendar.MINUTE, 59);
    calendar.set(Calendar.SECOND, 30);
    Date time = calendar.getTime();
    timerVideoRun.schedule(new saveTask(this), time);//schedule task on above time

    //vlcj load
    loadVLCJ();
    mediaPlayerComponent = new EmbeddedMediaPlayerComponent();

    //label time settings
    timer = new Timer(1000, new MyListener(this)); //Tick every 1000ms, let MyListener listen to the ticks
    timer.start(); //Start the timer
    lblTime.setFont(new Font("Helvetica", Font.PLAIN, 270));
    lblTime.setVerticalAlignment(SwingConstants.CENTER);
    lblTime.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    updateLabelTime();

    //JFrame
    add(lblTime, BorderLayout.CENTER);
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    setUndecorated(true);
    setResizable(false);
    setVisible(true);
    setExtendedState(JFrame.MAXIMIZED_BOTH); 
    mediaPlayerComponent.getMediaPlayer().prepareMedia(countdownVideo,":start-time=30");
}

public void updateLabelTime()
{
    now.setTime(System.currentTimeMillis());
    lblTime.setText(dateFormatter.format(now));

}

public void playVideo()
{
    timer.stop();
    setContentPane(mediaPlayerComponent);
    validate();
    mediaPlayerComponent.getMediaPlayer().play();

}

public void loadVLCJ()
{
    NativeLibrary.addSearchPath(
            RuntimeUtil.getLibVlcLibraryName(), vlcFolder
        );
        Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class);
}

}

public class MyListener implements ActionListener {

    TimerFrame frame;
    public MyListener(TimerFrame timerFrame) {
        this.frame = timerFrame;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        this.frame.updateLabelTime();
    }

}

public class saveTask extends TimerTask {

    TimerFrame frame;
    public saveTask(TimerFrame timerFrame) {
        this.frame = timerFrame;
    }

    @Override
    public void run() {
        this.frame.playVideo();

    }

}


public class TimerMain {

    /**
     * @param args
     */
    public static void main(String[] args) {
         SwingUtilities.invokeLater(new Runnable() {
             @Override
             public void run() {
                 new TimerFrame();
             }
         });


    }

}

我想问一下,如果我在 playVideo() 方法中注释掉 validate() 方法,程序不会“切换”到视频中,有人知道为什么会发生这种情况吗?比调用 validate() 更好的方法?

另外,如果有人在 VLCJ 方面有经验,是否有可能在视频中经过一定时间后停止播放音频,然后我会播放另一个音频?

【问题讨论】:

  • 我们不是在这里为您工作和/或研究,我们只是在这里为您提供与编码相关问题的帮助。
  • 嗨,对不起,以我目前的经验,我能想到的只是使用 JLabel 作为倒数计时器。你知道我可以向其他 StackExchange 寻求什么编程想法吗
  • 看看How to use Swing Timers 和this example of using JodaTime 来计算两个日期/时间之间的差异...

标签: java swing vlcj


【解决方案1】:

正如@Vallentin 所说,我们不是来为您工作或研究的。不过,至少我会非常乐意为您指明正确的方向。

如果您想要动画和所有内容,则必须研究图形。至于倒计时本身,请创建一个方法,在倒计时的每一秒内将setText() 方法应用于您的标签。

如果您开始编写代码并遇到错误,请随时发布您的一些代码,告诉我们错误,并告诉我们您要完成的工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-16
    • 2015-04-04
    • 2015-03-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多