Repository navigation
Expand file tree
/
Copy pathPlaylist.java
More file actions
172 lines (140 loc) · 3.3 KB
/
Copy pathPlaylist.java
File metadata and controls
172 lines (140 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import java.util.ArrayList;
import java.util.Random;
/**
* Stores a playlist of songs.
*
* @author Michael Nipper
*
*/
public class Playlist {
private ArrayList<Song> playlist;
private ArrayList<Song> playlistUnshuffled;
private String directory = "";
private int index = 0;
/**
* Generate a playlist for a given directory.
*
* @param dir
* A directory path containing Mp3s to use as a playlist.
*/
public Playlist(String dir) {
directory = dir;
playlist = new ArrayList<Song>();
FileLoader songLoader = new FileLoader(dir);
for (int i = 0; i < songLoader.getFileList().size(); i++) {
Song newSong = new Song(songLoader.getFileList().get(i));
playlist.add(newSong);
}
}
/**
* Get the playlist (an ArrayList of song objects).
*
* @return The ArrayList of song objects.
*/
public ArrayList<Song> getPlaylist() {
return playlist;
}
/**
* Get a song object by its index in the playlist.
*
* @param i
* The index of the song object to get.
* @return The requested song object.
*/
public Song getSongByIndex(int i) {
return playlist.get(i);
}
/**
* Get the current song object being played in the playlist.
*
* @return The song object being played.
*/
public Song getSong() {
return playlist.get(index);
}
/**
* Skip a song in the playlist.
*
* @return The song object of the next song.
*/
public Song skipSong() {
if (index < playlist.size() - 1)
index++;
else
index = 0;
return playlist.get(index);
}
/**
* Goes to the previous song in the playlist
*
* @return The new song object.
*/
public Song previousSong() {
if (index > 0)
index--;
else
index = playlist.size() - 1;
return playlist.get(index);
}
public int getIndex() {
return index;
}
/**
* Get the current directory.
*
* @return The current directory.
*/
public String getDirectory() {
return directory;
}
/**
* Set a new directory for the playlist.
*
* @param dir
* A directory location containing mp3 files.
*/
public void setDirectory(String dir) {
directory = dir;
}
/**
* Print the playlist filepaths to the console.
*/
public String toString() {
String returnString = "";
for (int i = 0; i < playlist.size(); i++) {
returnString += playlist.get(i).getFilePath() + "\n";
}
return returnString;
}
/**
* Get a random song from the playlist.
*
* @return A random song object.
*/
public Song getRandomSong() {
Random randomNumberGenerator = new Random();
int randomSong = randomNumberGenerator.nextInt(playlist.size());
index = randomSong;
return playlist.get(randomSong);
}
/**
* Shuffle the playlist.
*/
public void shuffleList() {
playlistUnshuffled = playlist;
for (int i = 0; i < playlist.size(); i++) {
Random randomNumberGenerator = new Random();
int randomSong = randomNumberGenerator.nextInt(playlist.size());
Song temp = playlist.get(i);
playlist.set(i, playlist.get(randomSong));
playlist.set(randomSong, temp);
}
index = 0;
}
/**
* Unshuffle the playlist.
*/
public void unshuffleList() {
playlist = playlistUnshuffled;
}
}