-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathMediaPlayer.java
More file actions
65 lines (55 loc) · 1.32 KB
/
MediaPlayer.java
File metadata and controls
65 lines (55 loc) · 1.32 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
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
//创建Player对象并扩展功能,由上一层操作,上一层实现播放,暂停,上一曲,下一曲,循环。随机,单曲
//主要功能
//1.播放 已实现
//2.暂停 已实现
//3.停止 已实现
//4.恢复播放 已实现
public class MediaPlayer {
private Player player;
public MediaPlayer() {
player = new Player();
}
// 返回是否是暂停,供上一层调用恢复播放功能
public boolean getPause() {
return player.isPause();
}
// 返回是否播放完毕,供上一层实现循环单曲下一曲等等方法
public boolean isStop() {
return player.IsStop();
}
// 传入字符串判断是URL或者File并调用底层播放器,此时底层播放器已在play(inputstream)中调用play方法
public void Play(String string) {
if (string.contains("http")) {
try {
player.play(new URL(string));
} catch (MalformedURLException e) {
e.printStackTrace();
}
} else {
player.play(new File(string));
}
}
// 恢复播放
public void Play() {
player.replay();
}
// 暂停
public void Pause() {
player.pause();
}
// 停止播放
public void Stop() {
try {
player.stop();
} catch (UnsupportedAudioFileException | IOException
| LineUnavailableException e) {
e.printStackTrace();
}
}
}