Repository navigation
Expand file tree
/
Copy pathMP3Player.java
More file actions
103 lines (84 loc) · 2.38 KB
/
Copy pathMP3Player.java
File metadata and controls
103 lines (84 loc) · 2.38 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
/*******************************************************************************
* Copyright (c) 2013 Yannic Remmet, Maximilian Berger.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Lesser Public License v2.1
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*
* Contributors:
* Yannic Remmet - initial API and implementation
* Maximilian Berger - initial API and implementation
******************************************************************************/
package player.mp3;
import java.net.URL;
import javazoom.jl.decoder.JavaLayerException;
import player.mp3.JLayerPlayerPausable.PlaybackListener;
public class MP3Player implements Runnable {
private URL filePath;
private JLayerPlayerPausable player;
private Thread playerThread;
private String namePlayerThread = "MP3PlayerThread";
public MP3Player(URL filePath) {
this.filePath = filePath;
}
public MP3Player(URL filePath, String namePlayerThread) {
this.filePath = filePath;
this.namePlayerThread = namePlayerThread;
}
public void play() {
if (player == null) {
this.playerInitialize();
} else if (!player.isPaused() || player.isComplete() || player.isStopped()) {
this.stop();
this.playerInitialize();
}
playerThread = new Thread(this, namePlayerThread);
//playerThread.setDaemon(true);
playerThread.start();
}
private void playerInitialize() {
try {
player = new JLayerPlayerPausable(filePath);
} catch (JavaLayerException e) {
e.printStackTrace();
}
}
public void pause() {
if (player != null) {
player.pause();
if (playerThread != null)
playerThread = null;
}
}
public void pauseToggle() {
if (player != null) {
if (player.isPaused() && !player.isStopped()) {
play();
} else
pause();
}
}
public void stop() {
if (player != null) {
player.stop();
if (playerThread != null)
playerThread = null;
}
}
public void run() {
try {
player.resume();
} catch (javazoom.jl.decoder.JavaLayerException ex) {
ex.printStackTrace();
}
}
public void setPlaybackListener(PlaybackListener newPlaybackListener){
player.setPlaybackListener(newPlaybackListener);
}
public boolean isPaused(){
return player.isPaused();
}
public boolean isStopped(){
return player.isStopped();
}
}