import java.applet.AudioClip;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.JApplet;
public class PlayMusic {
public static AudioClip loadSound(String filename) {
URL url = null;
try {
url = new URL("file:" + filename);
} catch (MalformedURLException e) {
}
return JApplet.newAudioClip(url);
}
public void play() {
AudioClip christmas = loadSound("music/bg.mp3");
christmas.play();
}
}
然后在主类中写
PlayMusic p=new PlayMusic();
p.play();
那就行了,其中bg.mp3是存放在是music包下的要播放的音乐文件
时间: 2024-10-02 16:42:38