嘿嘿,有天看电影,里面开机时有这样一个效果,后来稍稍实现了一下。
一,Master.java[主要用来实现音乐文件的播放]
/*
#(@)Master.java 20:18:25 1/8/2006
控制台打印时间及播放对应时间音乐。
*/
import java.io.*;
import java.net.*;
import java.applet.Applet;
import java.applet.AudioClip;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Properties;
public final class Master {
private static SimpleDateFormat style = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public Master() {}
/**
* 播放音乐及对用户控制台的输入做出反应
*/
private void hiMaster() {
if (this.helloMaster()) {
String s = this.read("提示:按任意键退出,R键重播...");
if (s != null && s.equalsIgnoreCase("r"))
this.hiMaster();
else
System.exit(0);
}
}
/**
* 播放音乐方法
* @return boolean 返回音乐是否播放成功
*/
private boolean helloMaster() {
boolean play = false;
Calendar cal = Calendar.getInstance();
String music = time(cal.getTime());
System.out.println("现在时间:" + style.format(cal.getTime()) + "。");
try {
URL u = new URL("file:///" + new File("audio").getAbsolutePath() + File.separator + music);
AudioClip ac = Applet.newAudioClip(u);
ac.play();
play = true;
} catch (MalformedURLException ex) {
printError(this.getClass().getName(), "helloMaster()", ex.getMessage());
}
return play;
}
/**
* 根据日期时间取得对应时间的音乐属性
* @param date Date 日期时间
* @return String 返回对应时间的音乐属性
*/
private String time(Date date) {
if (date == null) return null;
int y = date.getYear();
int m = date.getMonth();
int d = date.getDate();
InputStream is = this.getClass().getResourceAsStream("config.properties");
Properties p = new Properties();
try {
p.load(is);
int[] h = {6, 8, 12, 13, 18};
Date[] t = new Date[h.length];
for (int i=0; i<h.length; i++)
t[i] = new Date(y, m, d, h[i], 0);
if (date.before(t[2])) {
if (date.before(t[1])) {
return (date.before(t[0]))?p.getProperty("time.status.weehours"):p.getProperty("time.status.morning");
} else {
return p.getProperty("time.status.am");
}
} else {
if (date.before(t[4])) {
return (date.before(t[3]))?p.getProperty("time.status.noon"):p.getProperty("time.status.pm");
} else {
return p.getProperty("time.status.night");
}
}
} catch (IOException ex) {
printError(this.getClass().getName(), "time(Date d)", ex.getMessage());
return null;
}
}
/**
* 根据系统提示获取用户的控制台输入
* @param m String 系统提示信息
* @return String 返回用户的控制台输入
*/
private String read(String m) {
String s = null;
System.out.println(m);
try {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
s = in.readLine();
} catch (IOException ex) {
printError(this.getClass().getName(), "read(String s1)", ex.getMessage());
}
return s;
}
/**
* 打印错误消息
*/
private void printError(String c, String m, String e) {
System.out.println(style.format(Calendar.getInstance().getTime()) + ":调用"+ c + "." + m + "方法时发生错误:" + e + "。");
}
/**
* main()方法
*/
public static void main(String[] args) {
Master m = new Master();
m.hiMaster();
}
}
二,config.properties[配置文件,用来配置声音文件的名称]
#########################
### 播放声音配置 ###
#########################
# weehours 表示时间是凌晨
# morning 表示时间是早上
# am 表示时间是上午
# noon 表示时间是中午
# pm 表示时间是下午
# night 表示时间是晚上
time.status.weehours=weehours.wav
time.status.morning=morning.wav
time.status.am=am.wav
time.status.noon=noon.wav
time.status.pm=pm.wav
time.status.night=night.wav
三,audio目录及下面的文件,文件为以上命名方式。
转载于:https://www.cnblogs.com/civvy/archive/2007/09/11/889590.html