-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMusic.java
More file actions
62 lines (53 loc) · 1.64 KB
/
Copy pathMusic.java
File metadata and controls
62 lines (53 loc) · 1.64 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
package Mstw;
import javazoom.jl.player.Player;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
public class Music extends Thread{
private Player player;
private boolean isLoop;
private File file;
private FileInputStream fis;
private BufferedInputStream bis;
//重载构造函数,传入参数为名字和是否循环
public Music(String name, boolean isLoop){
try {
this.isLoop = isLoop;
//创建一个File对象
file = new File(Main.class.getResource("../music/"+name).toURI());
//创建一个输入流
fis = new FileInputStream(file);
//创建一个缓冲流
bis = new BufferedInputStream(fis);
//创建播放器对象,将文件的缓冲输入流传入进去
player = new Player(bis);
}
catch(Exception e){
e.printStackTrace();
}
}
public int getTime(){
if (player == null)
return 0;
return player.getPosition();
}
public void close(){
isLoop = false;
player.close();
this.interrupt();
}
@Override
public void run() {
try {
do{
player.play(); //用播放方法进行播放
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
player = new Player(bis);
}while(isLoop); //循环播放,直到有终止条件isLoop = false
}
catch(Exception e){
e.printStackTrace();
}
}
}