-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAudioList.java
More file actions
237 lines (202 loc) · 6.44 KB
/
Copy pathAudioList.java
File metadata and controls
237 lines (202 loc) · 6.44 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import java.util.ArrayList;
import javax.swing.Timer;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;
/**
* Makes an automatically-playing playlist of AudioClip files
* Supports wav, mp3, mp4, flv, aif, and more filetypes
* @author Kesav Viswanadha and Ofek Gila
* @version 2.3
* @since May 22nd, 2015
* @lastedited May 24th, 2015
*/
public class AudioList implements ActionListener {
public static final int NO_SHUFFLE = 0, CONSTANT_SHUFFLE = 1, INITIAL_SHUFFLE = 2;
public static final int CURRENT_FOLDER = 0, SUBFOLDERS_AND_CURRENT = 1, SEARCH_EVERYTHING = 2;
public static final String[] acceptedTypes = {"mp3", "wav", "mp4", "m4a", "m4v", "m3u8", "fxm", "flv", "aif", "aiff"};
public static final File currentFile = new File(System.getProperty("user.dir"));
private String[] locations;
private ArrayList<Integer> playlist;
private AudioClip clip;
private Timer loadNextSong;
private int songOn = 0;
private int shuffle;
private int folderSearch;
private double rate = 1;
public AudioList(Object[] locs, int shuffle) {
this.shuffle = shuffle;
locations = new String[locs.length];
for (int i = 0; i < locs.length; i++)
locations[i] = locs[i].toString();
playlist = new ArrayList<Integer>();
addNext();
clip = new AudioClip(new File(locations[playlist.get(songOn)]).getAbsoluteFile());
loadNextSong = new Timer((int)clip.length(), this);
loadNextSong.setRepeats(false);
}
public AudioList(ArrayList<String> locs, int shuffle) {
this(locs.toArray(), shuffle);
}
public AudioList() {
this(NO_SHUFFLE);
}
public AudioList(int shuffle, int folderSearch) {
this(shuffle, folderSearch, currentFile);
}
public AudioList(int shuffle) {
this(shuffle, acceptedTypes);
}
public AudioList(int shuffle, String... extensions) {
this(shuffle, 1, extensions);
}
public AudioList(int shuffle, int folderSearch, File startDirectory) {
this(shuffle, folderSearch, startDirectory, acceptedTypes);
}
public AudioList(int shuffle, int folderSearch, String... extensions) {
this(shuffle, folderSearch, currentFile, extensions);
}
public AudioList(int shuffle, int folderSearch, File startDirectory, String... extensions) {
this.folderSearch = folderSearch;
if (folderSearch == SEARCH_EVERYTHING)
while (startDirectory.getParentFile().getParentFile().getParentFile() != null)
startDirectory = startDirectory.getParentFile();
ArrayList<String> locs = getFilesInFolder(startDirectory, new ArrayList<String>(), extensions);
this.shuffle = shuffle;
locations = new String[locs.size()];
for (int i = 0; i < locs.size(); i++)
locations[i] = locs.get(i);
playlist = new ArrayList<Integer>();
addNext();
clip = new AudioClip(new File(locations[playlist.get(songOn)]).getAbsoluteFile());
loadNextSong = new Timer((int)clip.length(), this);
loadNextSong.setRepeats(false);
}
public ArrayList<String> getFilesInFolder(final File folder, ArrayList<String> locations, String... extensions) {
try {
for (final File fileEntry : folder.listFiles())
if (fileEntry.isDirectory() && (folderSearch == SUBFOLDERS_AND_CURRENT || folderSearch == SEARCH_EVERYTHING))
getFilesInFolder(fileEntry, locations, extensions);
else for (String extension : extensions)
if (getExtension(fileEntry.getPath()).equals(extension))
locations.add(fileEntry.getPath());
return locations;
} catch(NullPointerException e) {return locations; }
}
public String getDirectory() {
return System.getProperty("user.dir");
}
public String getExtension(String name) {
try {
return name.substring(name.lastIndexOf(".")).substring(1);
}
catch (StringIndexOutOfBoundsException e) {
return "";
}
}
public String getName(String loc) {
return loc.substring(loc.lastIndexOf("\\")+1, loc.lastIndexOf("."));
}
public void play() {
clip.setRate(rate);
clip.play();
loadNextSong.setInitialDelay((int)(clip.length() - clip.getPosition()));
loadNextSong.restart();
}
public void pause() {
clip.pause();
loadNextSong.setInitialDelay(Integer.MAX_VALUE);
loadNextSong.restart();
}
public void stop() {
clip.stop();
loadNextSong.setInitialDelay(Integer.MAX_VALUE);
loadNextSong.restart();
}
public void addNext() {
int ran;
if (playlist.size() == 0)
switch (shuffle) {
case NO_SHUFFLE:
playlist.add(0); break;
case CONSTANT_SHUFFLE: case INITIAL_SHUFFLE:
playlist.add((int)(Math.random() * locations.length)); break;
}
else switch(shuffle) {
case NO_SHUFFLE:
playlist.add((playlist.get(playlist.size() - 1) + 1) % locations.length); break;
case CONSTANT_SHUFFLE:
do ran = (int)(Math.random() * locations.length);
while (ran == playlist.get(playlist.size() - 1));
playlist.add(ran);
break;
case INITIAL_SHUFFLE:
if (playlist.size() >= locations.length)
playlist.add(playlist.get(playlist.size() - locations.length));
else {
do ran = (int)(Math.random() * locations.length);
while (inPlaylist(ran));
playlist.add(ran);
break;
}
}
}
public boolean inPlaylist(int location) {
for (Integer song : playlist)
if (song.intValue() == location)
return true;
return false;
}
public void nextSong() {
songOn++;
addNext();
clip.stop();
clip.dispose();
clip.reInit(new File(locations[playlist.get(songOn)]).getAbsoluteFile());
clip.setRate(rate);
clip.play();
loadNextSong.setInitialDelay((int)clip.length());
loadNextSong.restart();
}
public void setRate(double rate) {
this.rate = rate;
clip.setRate(rate);
loadNextSong.setInitialDelay((int)((loadNextSong.getDelay() - clip.getPosition()) / rate));
loadNextSong.restart();
}
public void previousSong() {
songOn--;
if (songOn < 0) songOn = playlist.size() - 1;
songOn--;
nextSong();
}
public void playSong(String songName) {
playlist = new ArrayList<Integer>();
songOn = 0;
clip.stop();
int songloc;
for (songloc = 0; songloc < locations.length; songloc++)
if (getName(locations[songloc]).equals(songName))
break;
if (songloc == locations.length)
System.err.println("Cannot find song " + songName);
else {
playlist.add(songloc);
clip.stop();
clip = new AudioClip(new File(locations[playlist.get(songOn)]).getAbsoluteFile());
play();
}
}
public boolean isPlaying() {
return clip.isRunning();
}
public void actionPerformed(ActionEvent e) {
nextSong();
}
public AudioClip getAudioClip() {
return clip;
}
public String getSongName() {
return clip.getName();
}
}