/
Mp3Player.java
89 lines (78 loc) · 1.82 KB
/
Mp3Player.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
import java.io.File;
import javax.media.*;
import javax.media.format.AudioFormat;
/**
* A Mp3 player class. Should be accessed through MediaController.
*
* @author Michael Nipper
*
*/
public class Mp3Player implements FilePlayer {
private Song currentSong;
private Player player;
private Time mediaTime;
private boolean paused = false;
/**
* Create a Mp3Player object and attach a song object.
* @param song
* A song object for the Mp3Player to control.
*/
public Mp3Player(Song song) {
currentSong = song;
}
/**
* Default constructor.
*/
public Mp3Player() {}
/**
* Play the currently attached song object.
*/
public void play() {
if (paused) {
player.setMediaTime(mediaTime);
player.start();
paused = false;
} else {
Format input1 = new AudioFormat(AudioFormat.MPEGLAYER3);
Format output = new AudioFormat(AudioFormat.MPEGLAYER3);
PlugInManager.addPlugIn(
"com.sun.media.codec.audio.mp3.JavaDecoder",
new Format[] { input1 }, new Format[] { output },
PlugInManager.CODEC);
try {
player = Manager.createPlayer(new MediaLocator(new File(
currentSong.getFilePath()).toURI().toURL()));
player.start();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
/**
* Stop the currently attached song object.
*/
public void stop() {
player.stop();
paused = false;
}
/**
* Set a song object to this Mp3Player.
*/
public void setSong(Song song) {
currentSong = song;
}
/**
* Pause the currently attached song object.
*/
public void pause() {
paused = true;
mediaTime = player.getMediaTime();
player.stop();
}
/**
* Test if the Mp3Player object is currently paused.
*/
public boolean isPaused() {
return paused;
}
}