Repository navigation
Expand file tree
/
Copy pathMusikMechanic.java
More file actions
144 lines (133 loc) · 4.43 KB
/
Copy pathMusikMechanic.java
File metadata and controls
144 lines (133 loc) · 4.43 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
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import javax.sound.sampled.*;
public class MusikMechanic extends Musik {
public static void play(Scanner in, Pengguna pengguna, String yangDiputar) throws UnsupportedAudioFileException, IOException, LineUnavailableException{
File[] file = new File[30];
int i = 0;
if(yangDiputar.equals("Playlist Spokify")){
if(DaftarLaguSpookify.list.isEmpty()){
System.out.println("Playlist Kosong");
return;
}
if(pengguna.statusKeanggotaan.equals("Free")){
for(Musik m : DaftarLaguSpookify.list) {
if(m.jenisLagu.equals("Old School")) {
file[i++] = new File("./DaftarLagu/OldSchool/"+m.getJudul() + ".wav");
}
}
} else if(pengguna.statusKeanggotaan.equals("Premium")){
for(Musik m : DaftarLaguSpookify.list) {
if(m.jenisLagu.equals("Old School")) {
file[i++] = new File("./DaftarLagu/OldSchool/"+m.getJudul() + ".wav");
}
if(m.jenisLagu.equals("New School")) {
file[i++] = new File("./DaftarLagu/NewSchool/"+m.getJudul() + ".wav");
}
}
}
} else if(yangDiputar.equals("Playlist Pengguna")){
if(pengguna.playlist.list.isEmpty()) {
System.out.println("Playlist Kosong");
return;
}
if(pengguna.statusKeanggotaan.equals("Free")){
for(Musik m : pengguna.playlist.list) {
if(m.jenisLagu.equals("Old School")) {
file[i++] = new File("./DaftarLagu/OldSchool/"+m.getJudul() + ".wav");
}
}
} else if(pengguna.statusKeanggotaan.equals("Premium")){
for(Musik m : pengguna.playlist.list) {
if(m.jenisLagu.equals("Old School")) {
file[i++] = new File("./DaftarLagu/OldSchool/"+m.getJudul() + ".wav");
}
if(m.jenisLagu.equals("New School")) {
file[i++] = new File("./DaftarLagu/NewSchool/"+m.getJudul() + ".wav");
}
}
}
}
int banyakLagu = hitungTotalLagu(file);
System.out.println("Daftar Lagu: ");
printList(file);
System.out.print("Pilih nomor lagu yang ingin Anda putar: ");
int pilihan = in.nextInt();
String response = "";
if(pilihan > banyakLagu) {
System.out.println("Nomor lagu tidak ada");
response = "D";
}
while(true) {
if(response.equals("Q")) {
return;
} else if(response.equals("D")) {
System.out.println("Daftar Lagu: ");
printList(file);
System.out.print("Pilih nomor lagu yang ingin Anda putar: ");
pilihan = in.nextInt();
} else if(response.equals("N")) {
if(pilihan == banyakLagu) {
pilihan = 1;
} else pilihan++;
}
response = "";
AudioInputStream audioStream = AudioSystem.getAudioInputStream(file[pilihan-1]);
Clip clip = AudioSystem.getClip();
clip.open(audioStream);
String fileName = file[pilihan-1].getName();
String judul[] = fileName.split(".wav");
System.out.println();
System.out.println("Playing " + DaftarLaguSpookify.cariMusik(judul[0]));
System.out.println(
"""
0:35 ━❍──────── -5:32
↻ ⊲ Ⅱ ⊳ ↺
VOLUME: ▁▂▃▄▅▆▇ 100%
""");
while(!response.equals("N") && !response.equals("Q") && !response.equals("D")) {
System.out.println("P = play, S = Stop, R = Reset, N = Next, M = Menu, Q = Quit");
System.out.print("Enter your choice: ");
response = in.next();
response = response.toUpperCase();
System.out.println(response);
switch(response) {
case ("P"): clip.start();
break;
case ("S"): clip.stop();
break;
case ("R"): clip.setMicrosecondPosition(0);
break;
case ("N"): clip.close();
break;
case ("D"): clip.close();
return;
case ("Q"): clip.close();
break;
default: System.out.println("Not a valid response");
}
System.out.println();
}
}
}
public static void printList(File[] file){
int i = 1;
for(File f: file){
if(f == null) break;
String fileName = f.getName();
String judul[] = fileName.split(".wav");
for(String s : judul) {
System.out.println((i++) +". " + s);
}
}
}
public static int hitungTotalLagu(File[] file){
int i = 0;
for(File f: file){
if(f == null) break;
i++;
}
return i;
}
}