-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMusicPlayer.java
220 lines (191 loc) · 5.25 KB
/
MusicPlayer.java
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
/**
* This music player can take in a file from an object. Extract the file name (source of the file), and find the file.
* The player hold all the necessary methods for play, pause and so on.
*
*
* @author fjoneus
*
*/
import java.util.*;
import java.io.*;
import javax.sound.sampled.*;
public class MusicPlayer {
private ItemSong songFile;
private Clip song;
private ArrayDeque<ItemSong> queue = new ArrayDeque<ItemSong>();
private boolean stopped = false;
private boolean active = false;
/**
* Creates a MusicPlayer object that can be used to play .wav music files. Note that this MusicPLayer uses an object called ItemSong that needs to hold the path to the .wav file.
* It can easily be modified to be used with other objects as long as it holds the information of the song file path.
* The MusicPlayer needs a GUI or some sort of main program that runs all the time, otherwise the MusicPLayer will stop when the main program has finished.
*/
public MusicPlayer() {
}
/**
* Creates a Clip object true AudioSystem, uses the open and start method from
* Clip to play the songFile. If the song has been on pause the song will start
* playing from currentPosition
*
* @exception catches all the exceptions and prints them.
*/
public void play() {
if (stopped == true) {
resume();
stopped = false;
} else {
try {
if (!queue.isEmpty() && active == false) {
song = AudioSystem.getClip();
songFile = queue.peek();
song.open(AudioSystem.getAudioInputStream(new File(queue.pollFirst().getSongFilePath()).getAbsoluteFile()));
song.start();
active = true;
}
} catch (Exception e) {
System.out.println(e);
}
}
}
/**
* Resumes the song from the current position if it has been stopped or on pause. You can mainly use the play method
* sinse it will now when to call the resume method if there has been a song on pause or stop.
*/
public void resume() {
song.start();
stopped = false;
active = true;
}
/**
* Saves the current position of the Clip so it can be used later with play/resume and then pauses the Clip.
*/
public void pause() {
song.stop();
stopped = true;
active = true;
}
/**
* Stops the Clip and sets the current position of the song to the beginning of the song.
*/
public void stop() {
song.stop();
song.setMicrosecondPosition(0);
stopped = true;
active = false;
}
/**
* Stops the song if one is active and plays the next, this will trigger the play method to get the
* next song in queue.
*/
public void nextSong() {
if (active)
stop();
stopped = false; //Overwrite the stop(); method that sets stop to true. This is so the play method dosen't resume instead of playing next song in queue.
play();
}
/**
* Terminate and closes the songs "Clip object". The data in the current song "Clip object" will be lost.
*/
public void terminate() {
song.close();
stopped = false;
active = false;
}
/**
* @param x sets current position of the song.
* In detail, pauses the song, sets the current time and then resumes the song from the new position.
*/
public void setTime(double x) {
song.stop();
long currentPos = (long) x * 1000000;
song.setMicrosecondPosition(currentPos);
if(!isStopped())
resume();
}
/**
* @return the current position of the song ("Clip") in seconds as int.
*/
public int getCurrentPos() {
return (int) song.getMicrosecondPosition() / 1000000;
}
/**
*
* @return the current songs length in seconds.
*/
public int getSongLength() {
return (int) song.getMicrosecondLength() / 1000000;
}
/**
*
* @param secs converts the value to time in seconds and minutes.
* @return the conversion as a String in format min : sec. Note, if the seconds less then
* 10 it will add a zero in front of the seconds.
*/
public String timeToString(int secs) {
String time;
int min = (int) secs / 60;
int sec = secs % 60;
if (sec < 10)
time = min + ":" + "0" + sec;
else
time = min + ":" + sec;
return time;
}
/**
*
* @param x adds the object to the song queue.
*/
public void addSongToQueue(ItemSong x) {
queue.add(x);
}
/**
* @param x adds the object first to the song queue.
*/
public void addSongFirstInQueue(ItemSong x) {
queue.addFirst(x);
}
/**
*
* @param vol sets the volume.
*/
public void setVolume(double vol) {
if (active && !stopped) {
if (vol < 0)
vol = 0;
else if (vol > 100)
vol = 100;
FloatControl volume = (FloatControl) song.getControl(FloatControl.Type.MASTER_GAIN);
float dB = (float) (Math.log(vol / 100) / Math.log(10.0) * 20.0);
volume.setValue(dB);
}
}
/**
*
* @return the current queue.
*/
public ArrayDeque<ItemSong> getQueue() {
return queue;
}
/**
*
* @return the current object that is being used.
*/
public ItemSong getCurrentSong() {
return songFile;
}
/**
*
* @return true or false if the song is stopped or not. If a song is stopped it is stopped, or on pause.
* Note that by default from start the method will return false.
*/
public boolean isStopped() {
return stopped;
}
/**
*
* @return true or false if the song is active or not. A song is active if the song being played.
*/
public boolean isActive() {
return active;
}
}