java多线程    Java入门    vsftp    ftp    linux配置    centos    FRP教程    HBase    Html5缓存    webp    zabbix    分布式    neo4j图数据库    

Java 编写倒计时闹钟定时播放音乐

月小升最近把安静的工作时间,调整为45分钟,苦于一时没有找到mac电脑上合适的闹钟,就用java整了一个
姑且用着,打包成time.jar

算法说明:


过了45分钟,响起音乐

使用办法:

java -jar time.jar

源代码:

package com.javaer.time;
import java.io.InputStream;
import java.net.URL;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;
public class MyAudioPlayer {
    private URL url = null;// 音乐文件的URl
    private AudioStream as = null;// 播放器
    public MyAudioPlayer() {
        try {
            //url = MyAudioPlayer.class.getResource("/yinxiao.mp3");// 获取音乐文件的url(改成你自己的音乐文件)
            url = new URL("file:/Users/ge/ge/web/java/time/yinxiao.wav");
            //System.out.println("XX:" + MyAudioPlayer.class.getResource("/"));
            InputStream is = url.openStream();// 获得音乐文件的输入流
            as = new AudioStream(is);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    // 播放音乐
    public void play() {
        AudioPlayer.player.start(as);// 用AudioPlayer静态成员player.start播放音乐
    }
    public static void main(String[] args) {

        System.out.println("sleep 45 minutes");
      try {
          Thread.sleep(45*60*1000);
      }catch(Exception ex){}
        new MyAudioPlayer().play();
    }
}

代码提交github
https://github.com/yuexiaosheng/time-machine


This entry was posted in JAVA and tagged . Bookmark the permalink.
月小升QQ 2651044202, 技术交流QQ群 178491360
首发地址:月小升博客https://java-er.com/blog/java-music-time/
无特殊说明,文章均为月小升原创,欢迎转载,转载请注明本文地址,谢谢
您的评论是我写作的动力.

Leave a Reply