1 package com.melloware.jukes.gui.tool;
2
3 import java.io.File;
4 import java.util.Map;
5
6 import javazoom.jlgui.basicplayer.BasicController;
7 import javazoom.jlgui.basicplayer.BasicPlayer;
8 import javazoom.jlgui.basicplayer.BasicPlayerEvent;
9 import javazoom.jlgui.basicplayer.BasicPlayerException;
10 import javazoom.jlgui.basicplayer.BasicPlayerListener;
11
12 import org.apache.commons.logging.Log;
13 import org.apache.commons.logging.LogFactory;
14
15 import com.jgoodies.uif.action.ActionManager;
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 public final class Player
32 implements BasicPlayerListener {
33
34 private static final Log LOG = LogFactory.getLog(Player.class);
35 private static final String ERROR_PAUSE_PLAY = Resources.getString("messages.ErrorPausingPlayingFile");
36 private float volume = MainModule.SETTINGS.getPlayerVolume();
37 private JukesPlayer player;
38 private long elapsedTime = 0;
39
40
41
42
43
44 public Player() {
45 super();
46 }
47
48
49
50
51
52
53 public JukesPlayer getPlayer() {
54
55 if (this.player == null) {
56 this.player = new JukesPlayer();
57
58
59 BasicPlayer.EXTERNAL_BUFFER_SIZE = MainModule.SETTINGS.getPlayerBufferSize();
60
61
62
63
64 this.player.addBasicPlayerListener(this);
65 }
66 return this.player;
67 }
68
69
70
71
72 public int getStatus() {
73 if (player == null) {
74 return -1;
75 } else {
76 return getPlayer().getStatus();
77 }
78 }
79
80
81
82
83
84
85 public float getVolume() {
86 return this.volume;
87 }
88
89
90
91
92
93
94
95 public void setController(BasicController controller) {
96 LOG.debug("setController : " + controller);
97 }
98
99
100
101
102
103
104 public void setVolume(float aVolume)
105 throws Exception {
106 this.volume = aVolume;
107 MainModule.SETTINGS.setPlayerVolume(aVolume);
108 if (getPlayer().hasGainControl()) {
109 getPlayer().setGain(aVolume);
110 }
111 }
112
113
114
115
116
117
118
119 public void fadeIn()
120 throws BasicPlayerException, InterruptedException {
121 if (MainModule.SETTINGS.isFadeInOnPlay()) {
122 for (float i = 0.0f; i < this.volume; i = i + 0.01f) {
123 getPlayer().setGain(i);
124 Thread.sleep(25);
125 }
126 }
127 }
128
129
130
131
132
133
134
135 public void fadeOut()
136 throws BasicPlayerException, InterruptedException {
137
138 for (double i = this.volume; i > 0.0f; i = i - 0.01f) {
139 getPlayer().setGain(i);
140 Thread.sleep(25);
141 }
142 }
143
144
145
146
147
148
149
150
151
152
153 public void opened(Object stream, Map properties) {
154
155
156 if (LOG.isDebugEnabled()) {
157 LOG.debug("opened : " + properties.toString());
158 }
159 }
160
161
162
163
164 public void pause() {
165 try {
166 if (getPlayer().getStatus() == BasicPlayer.PLAYING) {
167 if (MainModule.SETTINGS.isFadeOutOnPause()) {
168 fadeOut();
169 }
170 getPlayer().pause();
171 } else if (getPlayer().getStatus() == BasicPlayer.PAUSED) {
172 getPlayer().resume();
173 fadeIn();
174 }
175 } catch (BasicPlayerException ex) {
176 LOG.error(ERROR_PAUSE_PLAY, ex);
177 } catch (InterruptedException ex) {
178 LOG.error(ERROR_PAUSE_PLAY, ex);
179 } catch (Exception ex) {
180 LOG.error(ERROR_PAUSE_PLAY, ex);
181 }
182 }
183
184
185
186
187 public void play() {
188 try {
189 if (getPlayer().getStatus() == BasicPlayer.PAUSED) {
190 getPlayer().resume();
191 fadeIn();
192 } else if (getPlayer().getStatus() != BasicPlayer.STOPPED) {
193
194 if (MainModule.SETTINGS.isFadeOutOnStop()) {
195 fadeOut();
196 }
197 getPlayer().stop();
198 getPlayer().play();
199 } else {
200 getPlayer().play();
201 }
202 } catch (BasicPlayerException ex) {
203 LOG.error(ERROR_PAUSE_PLAY, ex);
204 } catch (InterruptedException ex) {
205 LOG.error(ERROR_PAUSE_PLAY, ex);
206 } catch (Exception ex) {
207 LOG.error(ERROR_PAUSE_PLAY, ex);
208 }
209 }
210
211
212
213
214
215
216 public void play(String filename) {
217 if (LOG.isDebugEnabled()) {
218 LOG.debug("Play " + filename);
219 }
220 try {
221
222 if (player == null) {
223 player = new JukesPlayer();
224
225
226
227
228 player.addBasicPlayerListener(this);
229 }
230
231
232 if (player.getStatus() != BasicPlayer.STOPPED) {
233 if ((player.getStatus() == BasicPlayer.PLAYING) && (MainModule.SETTINGS.isFadeOutOnChange())) {
234 fadeOut();
235 }
236 player.stop();
237 }
238
239 player.open(new File(filename));
240
241
242
243
244
245
246
247
248
249
250
251
252
253 if (player.hasPanControl()) {
254 player.setPan(0.0);
255 }
256 if (player.hasGainControl()) {
257 setVolume(volume);
258 }
259 player.play();
260 } catch (BasicPlayerException ex) {
261 LOG.error(Resources.getString("messages.ErrorPlayingFile"), ex);
262 } catch (InterruptedException ex) {
263 LOG.error(Resources.getString("messages.ErrorPlayingFile"), ex);
264 } catch (Exception ex) {
265 LOG.error(Resources.getString("messages.ErrorPlayingFile"), ex);
266 }
267 }
268
269
270
271
272
273
274
275
276
277
278
279
280
281 public void progress(int bytesread, long microseconds, byte[] pcmdata, Map properties) {
282
283
284
285
286 this.elapsedTime = microseconds;
287 }
288
289
290
291
292 public void resume() {
293 try {
294 LOG.debug("Resume");
295 getPlayer().resume();
296 } catch (BasicPlayerException ex) {
297 LOG.error(Resources.getString("messages.ErrorResumingFile"), ex);
298 } catch (Exception ex) {
299 LOG.error(Resources.getString("messages.ErrorResumingFile"), ex);
300 }
301 }
302
303
304
305
306
307
308 public void seek(float aPosition) {
309 LOG.debug("Seeking");
310 try {
311 getPlayer().seek((long)aPosition);
312 } catch (BasicPlayerException ex) {
313 LOG.error(Resources.getString("messages.ErrorSeekingFile"), ex);
314 } catch (Exception ex) {
315 LOG.error(Resources.getString("messages.ErrorSeekingFile"), ex);
316 }
317 }
318
319
320
321
322
323
324 public void stateUpdated(BasicPlayerEvent event) {
325 if (LOG.isDebugEnabled()) {
326 LOG.debug("stateUpdated : " + event.toString());
327 }
328 try {
329 switch (event.getCode()) {
330 case BasicPlayerEvent.OPENED:
331 case BasicPlayerEvent.PLAYING: {
332 ActionManager.get(Actions.PLAYER_PLAY_ID).setEnabled(false);
333 ActionManager.get(Actions.PLAYER_PAUSE_ID).setEnabled(true);
334 ActionManager.get(Actions.PLAYER_STOP_ID).setEnabled(true);
335 break;
336 }
337 case BasicPlayerEvent.RESUMED: {
338 ActionManager.get(Actions.PLAYER_PLAY_ID).setEnabled(false);
339 ActionManager.get(Actions.PLAYER_PAUSE_ID).setEnabled(true);
340 ActionManager.get(Actions.PLAYER_STOP_ID).setEnabled(true);
341 break;
342 }
343 case BasicPlayerEvent.PAUSED: {
344 ActionManager.get(Actions.PLAYER_PLAY_ID).setEnabled(true);
345 ActionManager.get(Actions.PLAYER_PAUSE_ID).setEnabled(false);
346 ActionManager.get(Actions.PLAYER_STOP_ID).setEnabled(true);
347 break;
348 }
349 case BasicPlayerEvent.STOPPED: {
350 ActionManager.get(Actions.PLAYER_PLAY_ID).setEnabled(true);
351 ActionManager.get(Actions.PLAYER_PAUSE_ID).setEnabled(false);
352 ActionManager.get(Actions.PLAYER_STOP_ID).setEnabled(false);
353 break;
354 }
355 default: {
356 break;
357 }
358 }
359 } catch (Exception ex) {
360 LOG.error("Exception", ex);
361 }
362 }
363
364
365
366
367 public void stop() {
368 try {
369 LOG.debug("Stop");
370 switch (getPlayer().getStatus()) {
371 case BasicPlayer.PLAYING:
372 case BasicPlayer.PAUSED: {
373 if (MainModule.SETTINGS.isFadeOutOnStop()) {
374 fadeOut();
375 }
376 getPlayer().stop();
377 break;
378 }
379 default: {
380 break;
381 }
382 }
383 } catch (BasicPlayerException ex) {
384 LOG.error(Resources.getString("messages.ErrorStoppingFile"), ex);
385 } catch (InterruptedException ex) {
386 LOG.error(Resources.getString("messages.ErrorStoppingFile"), ex);
387 } catch (Exception ex) {
388 LOG.error(Resources.getString("messages.ErrorStoppingFile"), ex);
389 }
390 }
391
392
393
394
395
396
397 public long getElapsedTime() {
398 return (this.elapsedTime/1000);
399 }
400
401 }