adding some test cases for the event queue
This commit is contained in:
@@ -24,12 +24,9 @@
|
||||
package com.iluwatar.event.queue;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
import javax.sound.sampled.AudioInputStream;
|
||||
import javax.sound.sampled.AudioSystem;
|
||||
import javax.sound.sampled.UnsupportedAudioFileException;
|
||||
|
||||
/**
|
||||
@@ -51,17 +48,12 @@ public class App {
|
||||
* @throws UnsupportedAudioFileException when the loaded audio file is unsupported
|
||||
*/
|
||||
public static void main(String[] args) throws UnsupportedAudioFileException, IOException {
|
||||
Audio.playSound(getAudioStream("./etc/Bass-Drum-1.wav"), -10.0f);
|
||||
Audio.playSound(getAudioStream("./etc/Closed-Hi-Hat-1.wav"), -8.0f);
|
||||
Audio.playSound(Audio.getAudioStream("./etc/Bass-Drum-1.wav"), -10.0f);
|
||||
Audio.playSound(Audio.getAudioStream("./etc/Closed-Hi-Hat-1.wav"), -8.0f);
|
||||
|
||||
System.out.println("Press Enter key to stop the program...");
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
|
||||
br.read();
|
||||
Audio.stopService();
|
||||
}
|
||||
|
||||
public static AudioInputStream getAudioStream(String filePath)
|
||||
throws UnsupportedAudioFileException, IOException {
|
||||
return AudioSystem.getAudioInputStream(new File(filePath).getAbsoluteFile());
|
||||
}
|
||||
}
|
||||
|
@@ -23,12 +23,15 @@
|
||||
|
||||
package com.iluwatar.event.queue;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.lang.Thread.State;
|
||||
|
||||
import javax.sound.sampled.AudioInputStream;
|
||||
import javax.sound.sampled.AudioSystem;
|
||||
import javax.sound.sampled.Clip;
|
||||
import javax.sound.sampled.LineUnavailableException;
|
||||
import javax.sound.sampled.UnsupportedAudioFileException;
|
||||
|
||||
/**
|
||||
* This class implements the Event Queue pattern.
|
||||
@@ -55,6 +58,17 @@ public class Audio {
|
||||
updateThread.interrupt();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This method stops the Update Method's thread.
|
||||
* @return boolean
|
||||
*/
|
||||
public static boolean isServiceRunning() {
|
||||
if (updateThread != null) {
|
||||
return updateThread.isAlive();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts the thread for the Update Method pattern if it was not started previously.
|
||||
@@ -86,17 +100,17 @@ public class Audio {
|
||||
init();
|
||||
// Walk the pending requests.
|
||||
for (int i = headIndex; i != tailIndex; i = (i + 1) % MAX_PENDING) {
|
||||
if (pendingAudio[i].stream == stream) {
|
||||
if (getPendingAudio()[i].stream == stream) {
|
||||
// Use the larger of the two volumes.
|
||||
pendingAudio[i].volume = Math.max(volume, pendingAudio[i].volume);
|
||||
getPendingAudio()[i].volume = Math.max(volume, getPendingAudio()[i].volume);
|
||||
|
||||
// Don't need to enqueue.
|
||||
return;
|
||||
}
|
||||
}
|
||||
pendingAudio[tailIndex] = new PlayMessage();
|
||||
pendingAudio[tailIndex].stream = stream;
|
||||
pendingAudio[tailIndex].volume = volume;
|
||||
getPendingAudio()[tailIndex] = new PlayMessage();
|
||||
getPendingAudio()[tailIndex].stream = stream;
|
||||
getPendingAudio()[tailIndex].volume = volume;
|
||||
tailIndex = (tailIndex + 1) % MAX_PENDING;
|
||||
}
|
||||
|
||||
@@ -112,7 +126,7 @@ public class Audio {
|
||||
Clip clip = null;
|
||||
try {
|
||||
clip = AudioSystem.getClip();
|
||||
clip.open(pendingAudio[headIndex].stream);
|
||||
clip.open(getPendingAudio()[headIndex].stream);
|
||||
clip.start();
|
||||
headIndex++;
|
||||
} catch (LineUnavailableException e) {
|
||||
@@ -123,4 +137,25 @@ public class Audio {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the AudioInputStream of a file
|
||||
* @param filePath is the path of the audio file
|
||||
* @return AudioInputStream
|
||||
* @throws UnsupportedAudioFileException when the audio file is not supported
|
||||
* @throws IOException when the file is not readable
|
||||
*/
|
||||
public static AudioInputStream getAudioStream(String filePath)
|
||||
throws UnsupportedAudioFileException, IOException {
|
||||
return AudioSystem.getAudioInputStream(new File(filePath).getAbsoluteFile());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns with the message array of the queue
|
||||
* @return PlayMessage[]
|
||||
*/
|
||||
public static PlayMessage[] getPendingAudio() {
|
||||
return pendingAudio;
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user