【发布时间】: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 来计算两个日期/时间之间的差异...