Reputation: 133
I've been writing a program that will take an array of the names of music files and play them. I succeeded in doing that, but, I wanted to touch up on some things and make it a little nicer. I am trying to make the music play in a random order but not repeat any songs before the entire list has been played. I was almost able to do it but I think there is something wrong with my do-while loop. The program runs as intended for about eight songs but then it stops playing the music and the JVM continues to run. I am using BlueJ as I am still an AP Comp Sci student so I realize that I might not be able to accomplish this task but any help would be greatly appreciated. I have a driver, "MusicDriver" that is a "has-a" relationship with two other classes: "MP3" and "Music".
my MP3 class:
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();
}
}
my Music class:
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;
}
}
my MusicDriver class:
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();
}
}
I have done a lot of research on why my program just stops playing my songs but I haven't been able to find anything. I did, find that BlueJ programs don't open the terminal window (the thing that comes up when you do a "System.out.print()") if you ask for input before you have any output but I don't think that comes into account for this program. I have also made sure that I have input a String "n" when I want to play the next song and for the first couple songs, it works, but after the eighth song, it just stops. I am thoroughly confused.
Upvotes: 8
Views: 5261
Reputation: 1449
Sasha said it in the comments: use Collections.shuffle(). In practice that would look a lil' something like this:
In the Music class have a method to get all the songs:
public List<String> getSongs() {return music;}
The loop in MusicDriver would be along the lines of:
List<String> songs = song.getSongs();
do{
Collections.shuffle(songs);
for (String songToPly: songs) {
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");
mp3.stopMP3();
line = s.nextLine();
if (!line.equals("n")) break;
}
}while(line.equals("n"));
On a variable-naming note, naming the instance of your Music class as "song" (singular) is kind of confusing. Maybe call it "music" or at least "songs".
Upvotes: 3
Reputation: 1
What I would do is:
public class MainClass() {
public static void main(String[] args) {
PlayerWrapper player = new PlayerWrapper();
}
}
public class PlayerWrapper() {
private List<MP3> playlist;
private Scanner userInputReader;
private String currentUserInput;
public PlayerWrapper() {
userInputReader = new Scanner(System.in());
System.out.println("Filepath to playlist?");
String playlistFileName = userInputReader.nextLine();
playlist = PlayListExtractor.extractPlaylist(playlistFileName);
start();
}
public void start() {
playlistCopy = new ArrayList<MP3>(playlist);
shufflePlayList(playlistCopy);
Iterator<MP3> songIterator = playlistCopy.iterator();
while (songIterator.hasNext()) {
MP3 song = songIterator.next();
songIterator.remove();
player = new Player(song.toStream());
player.play();
displayCurrentSongAndCommands(song);
currentUserInput = userInputReader.nextLine();
if ("Stop".equals(currentUserInput )) {
player.close();
break;
} else if ("n".equals(currentUserInput )) {
player.close();
continue;
}
}
if("Stop".equals(currentUserInput)) {
System.out.println("Playlist stopped. Press q to quit or c to continue");
currentUserInput = userInputReader.nextLine();
if ("q".equals(currentUserInput)) {
System.exit(0);
} else if ("c".equals(currentUserInput)) {
start();
}
}
start();
}
private void shufflePlayList(final List<MP3> playlistToBeShuffled) {
long seed = System.nanoTime();
Collections.shuffle(playlistToBeShuffled, new Random(seed));
}
private void displayCurrentSongAndCommands(final MP3 currentSong) {
System.out.println("Now Playing " + currentSong.toString());
System.out.println("Enter \"Stop\" to stop playing the song");
System.out.println("Enter \"n\" to play the next song");
}
}
public static class PlayListExtractor() {
private PlayListExtractor();
public static List<MP3> extractPlayList(final String playListFileName) {
List<MP3> result = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
String line;
while ((line = br.readLine()) != null) {
result.add(new MP3(line));
}
return result;
} catch (IOException e) {
System.out.println("Problem parsing playlist");
}
}
}
public class MP3 {
private String filename;
public MP3(final String filename) {
this.filename = filename;
}
public BufferedInputStream toStream() {
try {
FileInputStream fis = new FileInputStream(filename);
return new BufferedInputStream(fis);
}
catch (Exception e) {
System.out.println("Problem playing file " + filename);
System.out.println(e);
}
}
public String toString() {
return filename;
}
}
Upvotes: 0
Reputation: 1981
I think the only issue lies in the logic that you are using for shuffling the list.
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;
}
When the random number generated is already existing in the songNums list, you are generating a new random number. This new random number is not checked with all numbers of songNums list. The following change should solve your problem.
boolean done = false;
while(!done){
number = r.nextInt(song.size());
if(!songNum.contains(number)) done = true;
}
Alternatively, you can use Sasha's suggestion in the comments for shuffling the list(Collections.shuffle()).
Upvotes: 7
Reputation: 14580
The actual problem with your existing algorithm is you are not resetting counter
when you discover a song that has already been played. So as soon as you hit a repeat, you get stuck in an infinite loop - done
will never be true.
(Actually, it won't be infinite - once counter
reaches Integer.MAX_VALUE
it will wrap around to Integer.MIN_VALUE
and eventually reach 0
again, so if you left it long enough it would eventually play another song)
There are some useful suggestions here already about improvements to the code and I won't repeat them here, but the minimum change that will fix what you have is to move the initialization of counter
to 0
inside the loop:
boolean done = false;
while(!done){
int counter = 0; // reset counter every time
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;
}
Upvotes: 3