我一直在编写一个程序,它将获取一系列音乐文件的名称并播放它们.我成功地做到了这一点,但是,我想要修改一些东西,让它更好一些.我试图让音乐以随机顺序播放,但在整个列表播放之前不重复播放任何歌曲.我几乎能够做到,但我认为我的do-while循环有问题.该程序按预期运行大约八首歌曲,但随后它停止播放音乐,JVM继续运行.我正在使用BlueJ,因为我仍然是AP Comp Sci学生,所以我意识到我可能无法完成这项任务,但任何帮助将不胜感激.我有一个驱动程序,"MusicDriver",它与另外两个类别"有一个"关系:"MP3"和"音乐".
我的MP3课程:
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import javazoom.jl.player.Player;
public class MP3 {
String filename;
Player player;
public void stopMP3() { if (player != null) player.close(); }
// play the MP3 file to the sound card
public void playMP3(String filename) {
try {
FileInputStream fis = new FileInputStream(filename);
BufferedInputStream bis = new BufferedInputStream(fis);
player = new Player(bis);
}
catch (Exception e) {
System.out.println("Problem playing file " + filename);
System.out.println(e);
}
// run in new thread to play in background …
Run Code Online (Sandbox Code Playgroud)