Repository navigation
Expand file tree
/
Copy pathplayer.java
More file actions
190 lines (154 loc) · 5.81 KB
/
Copy pathplayer.java
File metadata and controls
190 lines (154 loc) · 5.81 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
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import java.util.LinkedList;
class musicPlayer {
Long currentFrame;
Clip clip;
String status;
static int i = 0;
AudioInputStream audioInputStream;
static String filePath;
public musicPlayer() throws UnsupportedAudioFileException, IOException, LineUnavailableException {
// create AudioInputStream object
audioInputStream = AudioSystem.getAudioInputStream(new File(filePath).getAbsoluteFile());
// create clip reference
clip = AudioSystem.getClip();
// open audioInputStream to the clip
clip.open(audioInputStream);
clip.loop(Clip.LOOP_CONTINUOUSLY);
}
public static void main(String[] args) {
File f1 = null;
try {
LinkedList<String> LLObj = new LinkedList<String>();
LLObj.push("/Users/rahulbhardwaj/Documents/Dev/Player/songs/Charlie Puth - Dangerously.wav");
LLObj.push("/Users/rahulbhardwaj/Documents/Dev/Player/songs/Eagles - Hotel California.wav");
LLObj.push("/Users/rahulbhardwaj/Documents/Dev/Player/songs/Charlie Puth - Hard on Yourself.wav");
LLObj.push("/Users/rahulbhardwaj/Documents/Dev/Player/songs/Britney Spears - Toxic.wav");
filePath = LLObj.get(i);
if(i == 0){
f1 = new File(LLObj.get(i));
System.out.println("## Now Playing " + f1.getName() + " ##");
}
musicPlayer audioPlayer = new musicPlayer();
audioPlayer.play();
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("1. Pause");
System.out.println("2. Resume");
System.out.println("3. Restart");
System.out.println("4. Jump to specific time");
System.out.println("5. Play next song");
System.out.println("6. Play previous song");
System.out.println("7. Exit");
int c = sc.nextInt();
if (c == 5){
audioPlayer.stop();
i++;
f1 = new File(LLObj.get(i));
System.out.println("## Now Playing " + f1.getName() + " ##");
musicPlayer.main(args);
}
else if (c == 6) {
audioPlayer.stop();
i--;
f1 = new File(LLObj.get(i));
System.out.println("## Now Playing " + f1.getName() + " ##");
musicPlayer.main(args);
}
audioPlayer.gotoChoice(c);
}
}
catch (Exception ex) {
System.out.println("Error with playing sound.");
ex.printStackTrace();
}
}
private void gotoChoice(int c) throws IOException, LineUnavailableException, UnsupportedAudioFileException {
switch (c) {
case 1:
pause();
break;
case 2:
resumeAudio();
break;
case 3:
restart();
break;
case 4:
System.out.println("Enter time (" + 0 + ", " + clip.getMicrosecondLength() + ")");
Scanner sc = new Scanner(System.in);
long c1 = sc.nextLong();
jump(c1);
break;
case 7:
stop();
System.exit(0);
}
}
// Method to play the audio
public void play() {
// start the clip
clip.start();
status = "play";
}
// Method to pause the audio
public void pause() {
if (status.equals("paused")) {
System.out.println("audio is already paused");
return;
}
this.currentFrame = this.clip.getMicrosecondPosition();
clip.stop();
status = "paused";
}
// Method to resume the audio
public void resumeAudio() throws UnsupportedAudioFileException, IOException, LineUnavailableException {
if (status.equals("play")) {
System.out.println("Audio is already " + "being played");
return;
}
clip.close();
resetAudioStream();
clip.setMicrosecondPosition(currentFrame);
this.play();
}
// Method to exit
public void stop() throws UnsupportedAudioFileException, IOException, LineUnavailableException {
currentFrame = 0L;
clip.stop();
clip.close();
}
// Method to jump over a specific part
public void jump(long c) throws UnsupportedAudioFileException, IOException, LineUnavailableException {
if (c > 0 && c < clip.getMicrosecondLength()) {
clip.stop();
clip.close();
resetAudioStream();
currentFrame = c;
clip.setMicrosecondPosition(c);
this.play();
}
}
// Method to restart the audio
public void restart() throws IOException, LineUnavailableException, UnsupportedAudioFileException {
clip.stop();
clip.close();
resetAudioStream();
currentFrame = 0L;
clip.setMicrosecondPosition(0);
this.play();
}
// Method to reset audio stream
public void resetAudioStream() throws UnsupportedAudioFileException, IOException, LineUnavailableException {
audioInputStream = AudioSystem.getAudioInputStream(new File(filePath).getAbsoluteFile());
clip.open(audioInputStream);
clip.loop(Clip.LOOP_CONTINUOUSLY);
}
}