-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.java
78 lines (70 loc) · 1.75 KB
/
Player.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
import java.io.*;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
public class Player extends Thread
{ private Thread sound;
private boolean stop;
private SourceDataLine line = null;
private int nBytesRead;
private String titre = "none";
public Player()
{ sound = new Thread(this);
sound.start();
stop = true;
}
public void start()
{ stop = false;
}
public void music()
{ File soundFile = new File(titre);
AudioInputStream audioInputStream = null;
try
{ audioInputStream = AudioSystem.getAudioInputStream(soundFile);
} catch (Exception e) {}
AudioFormat audioFormat = audioInputStream.getFormat();
DataLine.Info info = new DataLine.Info(SourceDataLine.class,audioFormat);
try
{ line = (SourceDataLine) AudioSystem.getLine(info);
line.open(audioFormat);
} catch (LineUnavailableException e) {}
catch (Exception e) {}
line.start();
nBytesRead = 0;
byte[] abData = new byte[128000];
while (nBytesRead != -1)
{ try
{ nBytesRead = audioInputStream.read(abData, 0, abData.length);
} catch (IOException e) {}
if (nBytesRead >= 0)
{ int nBytesWritten = line.write(abData, 0, nBytesRead);
}
}
line.drain();
line.stop();
}
public void setMusic(String nomSon)
{ titre = nomSon;
}
public String getMusic()
{ return titre;
}
public void fin()
{ nBytesRead = -1;
stop = true;
}
public void run()
{ while(sound.isAlive())
{ try
{ if(!stop)
{ music();
}
sleep(1);
}
catch (Exception e) {}
}
}
}