@@ -41,46 +41,51 @@ public class Audio {
41
41
42
42
private static final int MAX_PENDING = 16 ;
43
43
44
- private static int headIndex ;
44
+ private int headIndex ;
45
45
46
- private static int tailIndex ;
46
+ private int tailIndex ;
47
47
48
- private static Thread updateThread = null ;
48
+ private volatile Thread updateThread = null ;
49
49
50
- private static PlayMessage [] pendingAudio = new PlayMessage [MAX_PENDING ];
50
+ private PlayMessage [] pendingAudio = new PlayMessage [MAX_PENDING ];
51
+
52
+ // Visible only for testing purposes
53
+ Audio () {
54
+
55
+ }
56
+
57
+ public static Audio getInstance () {
58
+ return SingletonHolder .getAudioInstance ();
59
+ }
51
60
52
61
/**
53
- * This method stops the Update Method's thread.
62
+ * This method stops the Update Method's thread and waits till service stops.
54
63
*/
55
- public static synchronized void stopService () {
64
+ public synchronized void stopService () throws InterruptedException {
56
65
if (updateThread != null ) {
57
66
updateThread .interrupt ();
58
67
}
68
+ updateThread .join ();
69
+ updateThread = null ;
59
70
}
60
71
61
72
/**
62
73
* This method check the Update Method's thread is started.
63
74
* @return boolean
64
75
*/
65
- public static synchronized boolean isServiceRunning () {
66
- if (updateThread != null && updateThread .isAlive () ) {
67
- return true ;
68
- } else {
69
- return false ;
70
- }
76
+ public synchronized boolean isServiceRunning () {
77
+ return updateThread != null && updateThread .isAlive ();
71
78
}
72
79
73
80
/**
74
81
* Starts the thread for the Update Method pattern if it was not started previously.
75
82
* Also when the thread is is ready initializes the indexes of the queue
76
83
*/
77
- public static void init () {
84
+ public void init () {
78
85
if (updateThread == null ) {
79
- updateThread = new Thread (new Runnable () {
80
- public void run () {
81
- while (!Thread .currentThread ().isInterrupted ()) {
82
- Audio .update ();
83
- }
86
+ updateThread = new Thread (() -> {
87
+ while (!Thread .currentThread ().isInterrupted ()) {
88
+ update ();
84
89
}
85
90
});
86
91
}
@@ -90,7 +95,7 @@ public void run() {
90
95
/**
91
96
* This is a synchronized thread starter
92
97
*/
93
- public static synchronized void startThread () {
98
+ private synchronized void startThread () {
94
99
if (!updateThread .isAlive ()) {
95
100
updateThread .start ();
96
101
headIndex = 0 ;
@@ -103,7 +108,7 @@ public static synchronized void startThread() {
103
108
* @param stream is the AudioInputStream for the method
104
109
* @param volume is the level of the audio's volume
105
110
*/
106
- public static void playSound (AudioInputStream stream , float volume ) {
111
+ public void playSound (AudioInputStream stream , float volume ) {
107
112
init ();
108
113
// Walk the pending requests.
109
114
for (int i = headIndex ; i != tailIndex ; i = (i + 1 ) % MAX_PENDING ) {
@@ -123,7 +128,7 @@ public static void playSound(AudioInputStream stream, float volume) {
123
128
* This method uses the Update Method pattern.
124
129
* It takes the audio from the queue and plays it
125
130
*/
126
- public static void update () {
131
+ private void update () {
127
132
// If there are no pending requests, do nothing.
128
133
if (headIndex == tailIndex ) {
129
134
return ;
@@ -143,6 +148,7 @@ public static void update() {
143
148
e .printStackTrace ();
144
149
} catch (IllegalArgumentException e ) {
145
150
System .err .println ("The system doesn't support the sound: " + e .getMessage ());
151
+ e .printStackTrace ();
146
152
}
147
153
}
148
154
@@ -153,7 +159,7 @@ public static void update() {
153
159
* @throws UnsupportedAudioFileException when the audio file is not supported
154
160
* @throws IOException when the file is not readable
155
161
*/
156
- public static AudioInputStream getAudioStream (String filePath )
162
+ public AudioInputStream getAudioStream (String filePath )
157
163
throws UnsupportedAudioFileException , IOException {
158
164
return AudioSystem .getAudioInputStream (new File (filePath ).getAbsoluteFile ());
159
165
}
@@ -162,8 +168,15 @@ public static AudioInputStream getAudioStream(String filePath)
162
168
* Returns with the message array of the queue
163
169
* @return PlayMessage[]
164
170
*/
165
- public static PlayMessage [] getPendingAudio () {
171
+ public PlayMessage [] getPendingAudio () {
166
172
return pendingAudio ;
167
173
}
168
174
175
+ private static class SingletonHolder {
176
+ private static final Audio INSTANCE = new Audio ();
177
+
178
+ static Audio getAudioInstance () {
179
+ return INSTANCE ;
180
+ }
181
+ }
169
182
}
0 commit comments