-
Notifications
You must be signed in to change notification settings - Fork 6
/
MediaPlayers.java
174 lines (139 loc) · 4.08 KB
/
MediaPlayers.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
package potplayer;
import java.lang.reflect.InvocationTargetException;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.util.Duration;
import potplayer.ResourcesJvm;
class MediaPlayerSetting {
// Preset volume value is 0.5
static double lastVolume = 0.5;
static double muteBefore = 0.0;
MediaPlayerSetting() {
return;
}
static void setSaveVolume(double v) {
lastVolume = v;
}
static double getSaveVolume() {
return lastVolume;
}
static void setSaveMuteBefore(double v) {
muteBefore = v;
}
static double getSaveMuteBefore() {
return muteBefore;
}
}
public class MediaPlayers {
Media media;
MediaPlayer mediaPlayer;
Duration duration;
Duration playTime;
boolean mediaEnd = false;
public MediaPlayers(String fileName) {
this.media = new Media(fileName);
this.mediaPlayer = new MediaPlayer(this.media);
}
public void seekStart() {
this.mediaPlayer.seek(this.mediaPlayer.getStartTime());
}
public void play() {
this.mediaPlayer.play();
}
public void pause() {
this.mediaPlayer.pause();
}
public void stop() {
this.mediaPlayer.stop();
}
public MediaPlayer.Status getPlayerStatus() {
/**
* HALTED, UNKNOWN, PAUSED
* READY, STOPPED, PLAYING
*/
return this.mediaPlayer.getStatus();
}
public boolean checkMedia() {
if (this.media != null) {
return true;
// media is exists
} else {
return false;
// media not exists
}
}
public void setMediaEnd(boolean end) {
if (end == true) {
this.mediaEnd = true;
} else {
this.mediaEnd = false;
}
}
public boolean isMediaEnd() {
if (this.mediaEnd == true) {
return true;
} else {
return false;
}
}
public Duration getDuration() {
return this.mediaPlayer.getMedia().getDuration();
}
public Duration getPlaytime() {
return this.mediaPlayer.getCurrentTime();
}
public void setVolume(double volume) {
// double data type [0.0 -> 1.0]
this.mediaPlayer.setVolume(volume);
}
public double getVolume() {
// double data type [0.0 -> 1.0]
return this.mediaPlayer.getVolume();
}
public void setProgress(double progress) {
// double data type [0.0 -> 1.0]
Duration d = this.mediaPlayer.getMedia().getDuration();
this.mediaPlayer.seek(d.multiply(progress));
this.mediaPlayer.play();
}
public double getProgress() {
// double data type [0.0 -> 1.0]
// toMillis convert time to (ms)
return (this.getPlaytime().toMillis() / this.getDuration().toMillis());
}
public void setAudioRenew(double speed) {
// double data type [0.0 -> 1.0]
// set float[] magnitudes renew time
this.mediaPlayer.setAudioSpectrumInterval(speed);
}
public void releaseMemory() {
if (this.mediaPlayer.getStatus() == MediaPlayer.Status.PLAYING){
this.mediaPlayer.stop();
}
mediaPlayer.dispose();
this.media = null;
this.mediaPlayer = null;
ResourcesJvm.noticeJVM();
}
public void saveLastVolume(double value) {
// double data type [0.0 -> 1.0]
// save last media play volume
MediaPlayerSetting.setSaveVolume(value);
}
public double getLastVolume() {
// double data type [0.0 -> 1.0]
// get last media play volume
return MediaPlayerSetting.getSaveVolume();
}
public void saveMuteBefore(double value) {
// double data type [0.0 -> 1.0]
// mute before save player volute
MediaPlayerSetting.setSaveMuteBefore(value);
}
public double getMuteBefore() {
// double data type [0.0 -> 1.0]
// cancel player mute, get mute before volume,
// to set player volume
return MediaPlayerSetting.getSaveMuteBefore();
}
}