在使用swing做桌面端开发时,有时候需要播放一些音频
package com.course.util;
import java.io.InputStream;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;
/*
* @通用背景音乐播放模块
* @version 1.0
*/
public class MusicDemo {
public MusicDemo(){
init();
}
public void init() {
//方法三
InputStream in = null;
try {
in=this.getClass().getResourceAsStream("/com/course/sounds/logic.mid");
AudioStream as = new AudioStream(in);
AudioPlayer.player.start(as);
} catch (Exception e) {
e.printStackTrace();
}
}
}