-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCLI.java
More file actions
244 lines (205 loc) · 9.59 KB
/
CLI.java
File metadata and controls
244 lines (205 loc) · 9.59 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
238
239
240
241
242
243
244
package JCMusic;
import JCMusic.logic.Audio;
import JCMusic.logic.PlaylistCreator;
import JCMusic.logic.Playlists;
import java.util.Scanner;
import javafx.embed.swing.JFXPanel;
import javax.swing.SwingUtilities;
import javafx.application.Platform;
public class CLI {
public static int index;
public static boolean dirSpecified;
public static String playlistName;
private static boolean isPlaying = false;
private static Thread playbackThread;
public static void cliApp() {
new JFXPanel();
Audio audio = new Audio();
Playlists playlists = new Playlists();
Scanner scanner = new Scanner(System.in);
PlaylistCreator pc = new PlaylistCreator();
if (!dirSpecified) {
System.out.println("Enter playlist directory (*.cmpl, *.jcmpl): ");
playlistName = scanner.nextLine();
}
try {
playlists.loadPlaylist(playlistName);
} catch (Exception ex) {
ex.printStackTrace();
return;
}
index = 0;
if (playlistName != null) {
boolean exit = false;
clearConsole();
System.out.println("JCMPL");
System.out.println("-------");
System.out.println("Controls: 'p' = play, 'q' = quit");
System.out.println("Commands: 'editor' - enter playlist editor.");
while (!exit) {
System.out.print("> ");
String command = scanner.nextLine().trim().toLowerCase();
switch (command) {
case "p":
if (!isPlaying) {
if (playlists.songs == null || playlists.songs.length == 0) {
System.out.println("No playlist specified or it's empty! Please enter 'editor' or specify playlist.");
} else {
isPlaying = true;
playbackThread = new Thread(() -> {
while (isPlaying && index < playlists.songs.length) {
audio.playAudio(playlists.songs[index]);
int currentIndex = index;
SwingUtilities.invokeLater(() -> startTrack(audio, playlists, currentIndex));
// Wait until track ends
while (audio.isPlaying()) {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
return;
}
if (!isPlaying) break;
}
if (!isPlaying) break;
index++;
}
if (index >= playlists.songs.length) {
isPlaying = false;
System.out.println("\nReached end of playlist.");
}
});
playbackThread.start();
}
} else {
System.out.println("Already playing.");
}
break;
case "s":
if (isPlaying) {
isPlaying = false;
audio.stopAudio();
if (playbackThread != null) playbackThread.interrupt();
System.out.println("Stopped playback.");
} else {
System.out.println("Nothing is playing.");
}
break;
case "q":
if (isPlaying) {
isPlaying = false;
audio.stopAudio();
if (playbackThread != null) playbackThread.interrupt();
}
exit = true;
System.out.println("Exiting...");
break;
case "n":
if (index < playlists.songs.length - 1) {
audio.stopAudio();
index++;
isPlaying = true;
if (playbackThread != null && playbackThread.isAlive()) {
playbackThread.interrupt();
}
playbackThread = new Thread(() -> {
audio.playAudio(playlists.songs[index]);
int currentIndex = index;
SwingUtilities.invokeLater(() -> startTrack(audio, playlists, currentIndex));
while (audio.isPlaying()) {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
return;
}
}
index++;
if (index >= playlists.songs.length) {
isPlaying = false;
System.out.println("\nReached end of playlist.");
}
});
playbackThread.start();
}
break;
case "b":
if (index > 0) {
audio.stopAudio();
index--;
isPlaying = true;
if (playbackThread != null && playbackThread.isAlive()) {
playbackThread.interrupt();
}
playbackThread = new Thread(() -> {
audio.playAudio(playlists.songs[index]);
int currentIndex = index;
SwingUtilities.invokeLater(() -> startTrack(audio, playlists, currentIndex));
while (audio.isPlaying()) {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
return;
}
}
index++;
if (index >= playlists.songs.length) {
isPlaying = false;
System.out.println("\nReached end of playlist.");
}
});
playbackThread.start();
}
break;
case "editor":
System.out.println("Enter playlist name.");
System.out.print("> ");
String newPlaylistName = scanner.nextLine();
System.out.println("Enter song(s) directory.");
System.out.print("> ");
String directory = scanner.nextLine();
try {
pc.importSongs(newPlaylistName, directory);
} catch (Exception e) {
System.out.printf("Error importing songs: %s\n", e);
}
try {
playlists.loadPlaylist(newPlaylistName + ".jcmpl");
playlistName = newPlaylistName + ".jcmpl";
index = 0;
} catch (Exception ex) {
ex.printStackTrace();
return;
}
break;
default:
System.out.println("Unknown command. Use 'p', 's', 'q', 'n', 'b', or 'editor'.");
}
}
} else {
System.out.println("No playlist specified! Please enter 'editor' or specify playlist.");
}
scanner.close();
}
private static void restartTrack(Audio audio, Playlists playlists) {
if (isPlaying) {
isPlaying = false;
audio.stopAudio();
if (playbackThread != null) playbackThread.interrupt();
}
isPlaying = true;
playbackThread = new Thread(() -> {
audio.playAudio(playlists.songs[index]);
startTrack(audio, playlists, index);
});
playbackThread.start();
}
private static void startTrack(Audio audioInstance, Playlists playlistsInstance, int currentIndex) {
clearConsole();
System.out.println("Now playing: " + playlistsInstance.songs[currentIndex]);
System.out.printf("Track %d of %d\n", currentIndex + 1, playlistsInstance.songs.length);
System.out.println("Playlist: " + playlistName);
}
private static void clearConsole() {
System.out.print("\033[H\033[2J");
System.out.flush();
}
}