展开全部
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test {
private static String PLAYER_EXE = "C:/Program Files/Windows Media Player/wmplayer.exe";//播放器所在位置,不要有中文路62616964757a686964616fe58685e5aeb931333363356565径
private static String PLAYER_MUSIC = "D:/KuGou/1.mp3";//播放文件所在位置,不要有中文路径
private static final int TIME = 3;// 播放时长,单位:分钟
private static Process p = null;
public static void main(String[] args) throws IOException {
timer();
}
/**
* 创建播放定时器
*
* @throws IOException
*/
public static void timer() throws IOException {
final long timeInterval = 1000 * 60 * TIME;
Runnable runnable = new Runnable() {
public void run() {
while (true) {
try {
Mp3Player();
} catch (IOException e1) {
e1.printStackTrace();
}
try {
Thread.sleep(timeInterval);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
Thread thread = new Thread(runnable);
thread.start();
}
public static void Mp3Player() throws IOException {
// 第一个参数为播放器路径,第二个参数为播放音乐文件
String[] command = new String[] {PLAYER_EXE, PLAYER_MUSIC };
if (null == p) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
System.out.println("当前时间:" + df.format(new Date())+"正在播放设定音乐文件,"+ "将在"+TIME+"分钟后重新播放。感谢交流!");
p = Runtime.getRuntime().exec(command);
} else {
p.destroy();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
System.out.println("当前时间:" + df.format(new Date())+"正在播放设定音乐文件,"+ "将在"+TIME+"分钟后重新播放。感谢交流!");
p = Runtime.getRuntime().exec(command);
}
}
}