我正在编写一个程序,可以播放音乐文件名的数组。我已经成功实现了这个功能,但是想要增强一些功能并使它更加美观。我试图使音乐以随机顺序播放,但在整个列表播放完之前不重复任何歌曲。我几乎做到了,但我认为我的do-while循环有些问题。该程序在播放约八首歌曲后停止播放音乐,但JVM仍然在运行。由于我仍然是AP计算机科学学生,所以我使用BlueJ,意识到我可能无法完成此任务,但任何帮助将不胜感激。我有一个驱动器“MusicDriver”,与其他两个类“MP3”和“Music”存在“has-a”关系。
我已经对为什么我的程序停止播放歌曲进行了大量研究,但是我还没有找到任何结果。我确实发现,BlueJ程序在输出之前请求输入时不会打开终端窗口(当您执行“System.out.print()”时出现的东西),但我不认为这与该程序有关。我还确认了当我想播放下一首歌曲时输入了一个字符串“n”,并且在前几首歌曲中它起作用了,但在第八首歌曲后它就停止了。我非常困惑。
我的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
new Thread() {
public void run() {
try { player.play(); }
catch (Exception e) { System.out.println(e); }
}
}.start();
}
}
我的音乐课:
import java.util.*;
public class Music{
private ArrayList<String> music;
public Music(){music = new ArrayList<String>();}
public int size(){return music.size();}
public void addSong(String song){music.add(song);}
public String getSong(){return music.get(music.size());}
public String getSong(int num){return music.get(num);}
public void removeSong(String song){
for(int i = 0; i < music.size(); i++){
if(music.get(i).equals(song)) {music.remove(i); return;}
}
}
public String toString(){
String s = "";
for(int i = 0; i < music.size(); i++){
s += music.get(i);
}
return s;
}
}
我的MusicDriver类:
import java.util.*;
import java.io.*;
import javazoom.jl.player.Player;
import java.util.Random;
import java.util.Scanner;
import java.io.FileNotFoundException;
public class MusicDriver{
public static void main(String[] args) throws FileNotFoundException{
Random r = new Random();
Scanner s = new Scanner(System.in);
String line = "";
int number;
Music song = new Music();
song.addSong("1-01-overture.mp3");
song.addSong("1-03-fortune-teller-2.mp3");
song.addSong("1-07-prayer.mp3");
song.addSong("1-08-island-atlas.mp3");
song.addSong("1-12-warren-report.mp3");
song.addSong("1-13-avilla-hanya.mp3");
song.addSong("1-20-war-situation.mp3");
song.addSong("2-10-fog-of-phantom.mp3");
song.addSong("2-12-religious-precepts.mp3");
song.addSong("2-14-box-of-sentiment.mp3");
song.addSong("3-02-light-everlasting.mp3");
song.addSong("3-09-viking-spirits.mp3");
song.addSong("3-12-unsealed.mp3");
song.addSong("3-16-notice-of-death-reprise-.mp3");
//14 songs
ArrayList<Integer> songNums = new ArrayList<Integer>();
MP3 mp3 = new MP3();
do{
if(songNums.size() == song.size()) songNums.clear();
number = r.nextInt(song.size());
boolean done = false;
int counter = 0;
while(!done){
for(int i = 0; i < songNums.size(); i++){
if(number == songNums.get(i).intValue()) {number = r.nextInt(song.size()); counter++;}
}
if(counter == 0) done = true;
else done = false;
}
songNums.add(number);
mp3.playMP3(song.getSong(number));
System.out.println("Now Playing " + song.getSong(number));
System.out.println("Enter \"Stop\" to stop playing the song");
System.out.println("Enter \"n\" to play the next song");
line = s.nextLine();
mp3.stopMP3();
}while(line.equals("n"));
mp3.stopMP3();
}
}
我已经对为什么我的程序停止播放歌曲进行了大量研究,但是我还没有找到任何结果。我确实发现,BlueJ程序在输出之前请求输入时不会打开终端窗口(当您执行“System.out.print()”时出现的东西),但我不认为这与该程序有关。我还确认了当我想播放下一首歌曲时输入了一个字符串“n”,并且在前几首歌曲中它起作用了,但在第八首歌曲后它就停止了。我非常困惑。
Collections.shuffle()
方法对列表进行洗牌,然后在 for-each 循环中逐一播放歌曲。 - Alex SalauyouMP3#playMP3()
不是阻塞的,因此它会在歌曲开始播放后立即返回(当您不需要询问“播放下一首?”时)。 - Alex Salauyou