|
8 | 8 | import javax.sound.sampled.LineListener; |
9 | 9 | public class Audio |
10 | 10 | { |
11 | | - private Clip audioFile; |
12 | | - public String Path; |
13 | | - public boolean error = false; |
14 | | - public double ClipLength = 0; |
15 | | - public double ClipStart = 0; |
16 | | - public boolean play = false; |
17 | | - public boolean Loop = false; |
18 | | - public Audio(String fileName, double cS, double cL) |
19 | | - { |
20 | | - Path = fileName; |
21 | | - ClipStart = cS * 1000000; |
22 | | - ClipLength = cL * 1000000; |
23 | | - Refresh(); |
24 | | - } |
25 | | - public void Play() |
26 | | - { |
27 | | - new Thread(new Runnable() |
28 | | - { |
29 | | - public void run() |
30 | | - { |
31 | | - try |
32 | | - { |
33 | | - play = true; |
34 | | - audioFile.start(); |
35 | | - audioFile.addLineListener(new LineListener() |
36 | | - { |
37 | | - public void update(LineEvent event) |
38 | | - { |
39 | | - if (event.getType() != Type.STOP) |
40 | | - { |
41 | | - return; |
42 | | - } |
43 | | - //play = false; |
44 | | - } |
45 | | - }); |
46 | | - while ((audioFile.getMicrosecondLength() != audioFile.getMicrosecondPosition()) && (play)) |
47 | | - { |
48 | | - if ((ClipLength > 0) && (GetPosition() > ClipLength)) |
49 | | - { |
50 | | - audioFile.stop(); |
51 | | - play = false; |
52 | | - } |
53 | | - } |
54 | | - play = false; |
55 | | - Refresh(); |
56 | | - } |
57 | | - catch (Exception e) |
58 | | - { |
59 | | - |
60 | | - } |
61 | | - } |
62 | | - }).start(); |
63 | | - } |
64 | | - public void Stop() |
65 | | - { |
66 | | - audioFile.stop(); |
67 | | - play = false; |
68 | | - } |
69 | | - public boolean IsPlaying() |
70 | | - { |
71 | | - return audioFile.isRunning(); |
72 | | - } |
73 | | - public void Refresh() |
74 | | - { |
75 | | - new Thread(new Runnable() |
76 | | - { |
77 | | - public void run() |
78 | | - { |
79 | | - try |
80 | | - { |
81 | | - AudioInputStream ais = AudioSystem.getAudioInputStream(new File(Path)); |
82 | | - audioFile = AudioSystem.getClip(); |
83 | | - audioFile.open(ais); |
84 | | - audioFile.setMicrosecondPosition((long)ClipStart); |
85 | | - if (Loop == true) |
86 | | - { |
87 | | - Play(); |
88 | | - } |
89 | | - } |
90 | | - catch (Exception e) |
91 | | - { |
92 | | - e.printStackTrace(); |
93 | | - error = true; |
94 | | - } |
95 | | - } |
96 | | - }).start(); |
97 | | - } |
98 | | - public double GetPosition() |
99 | | - { |
100 | | - return audioFile.getMicrosecondPosition(); |
101 | | - } |
| 11 | + private Clip audioFile; |
| 12 | + public String Path; |
| 13 | + public boolean error = false; |
| 14 | + public double ClipLength = 0; |
| 15 | + public double ClipStart = 0; |
| 16 | + public boolean play = false; |
| 17 | + public boolean Loop = false; |
| 18 | + public Audio(String fileName, double cS, double cL) |
| 19 | + { |
| 20 | + Path = fileName; |
| 21 | + ClipStart = cS * 1000000; |
| 22 | + ClipLength = cL * 1000000; |
| 23 | + Refresh(); |
| 24 | + } |
| 25 | + public void Play() |
| 26 | + { |
| 27 | + new Thread(new Runnable() |
| 28 | + { |
| 29 | + public void run() |
| 30 | + { |
| 31 | + try |
| 32 | + { |
| 33 | + play = true; |
| 34 | + audioFile.start(); |
| 35 | + audioFile.addLineListener(new LineListener() |
| 36 | + { |
| 37 | + public void update(LineEvent event) |
| 38 | + { |
| 39 | + if (event.getType() != Type.STOP) |
| 40 | + { |
| 41 | + return; |
| 42 | + } |
| 43 | + //play = false; |
| 44 | + } |
| 45 | + }); |
| 46 | + while ((audioFile.getMicrosecondLength() != audioFile.getMicrosecondPosition()) && (play)) |
| 47 | + { |
| 48 | + if ((ClipLength > 0) && (GetPosition() > ClipLength)) |
| 49 | + { |
| 50 | + audioFile.stop(); |
| 51 | + play = false; |
| 52 | + } |
| 53 | + } |
| 54 | + play = false; |
| 55 | + Refresh(); |
| 56 | + } |
| 57 | + catch (Exception e) |
| 58 | + { |
| 59 | + |
| 60 | + } |
| 61 | + } |
| 62 | + }).start(); |
| 63 | + } |
| 64 | + public void Stop() |
| 65 | + { |
| 66 | + audioFile.stop(); |
| 67 | + play = false; |
| 68 | + } |
| 69 | + public boolean IsPlaying() |
| 70 | + { |
| 71 | + return audioFile.isRunning(); |
| 72 | + } |
| 73 | + public void Refresh() |
| 74 | + { |
| 75 | + new Thread(new Runnable() |
| 76 | + { |
| 77 | + public void run() |
| 78 | + { |
| 79 | + try |
| 80 | + { |
| 81 | + AudioInputStream ais = AudioSystem.getAudioInputStream(new File(Path)); |
| 82 | + audioFile = AudioSystem.getClip(); |
| 83 | + audioFile.open(ais); |
| 84 | + audioFile.setMicrosecondPosition((long)ClipStart); |
| 85 | + if (Loop == true) |
| 86 | + { |
| 87 | + Play(); |
| 88 | + } |
| 89 | + } |
| 90 | + catch (Exception e) |
| 91 | + { |
| 92 | + e.printStackTrace(); |
| 93 | + error = true; |
| 94 | + } |
| 95 | + } |
| 96 | + }).start(); |
| 97 | + } |
| 98 | + public double GetPosition() |
| 99 | + { |
| 100 | + return audioFile.getMicrosecondPosition(); |
| 101 | + } |
102 | 102 | } |
0 commit comments