diff --git a/sound/pen.wav b/sound/pen.wav new file mode 100644 index 0000000..4a76e6d Binary files /dev/null and b/sound/pen.wav differ diff --git a/src/DialogueMenu.java b/src/DialogueMenu.java index d8f6ac9..6ddb095 100644 --- a/src/DialogueMenu.java +++ b/src/DialogueMenu.java @@ -2,7 +2,10 @@ // displays dialogue, animates dialogue, and renders box that contains dialogue as well as the portraits of the characters // that speak to the players +import javax.sound.sampled.LineUnavailableException; +import javax.sound.sampled.UnsupportedAudioFileException; import java.awt.*; +import java.io.IOException; import java.io.Serializable; import java.util.ArrayList; @@ -101,6 +104,12 @@ public class DialogueMenu extends TextBox implements Serializable { if (frameCounter >= FREQUENCY) { frameCounter -= FREQUENCY; currentFrame += 1; + // play dialogue sound + try { + UtilityFunction.playSound("sound/pen.wav"); + } catch (UnsupportedAudioFileException | LineUnavailableException | IOException e) { + throw new RuntimeException(e); + } } // set font of string to be drawn g.setFont(font); diff --git a/src/Sound.java b/src/Sound.java index c8fa995..856351c 100644 --- a/src/Sound.java +++ b/src/Sound.java @@ -22,8 +22,7 @@ public class Sound implements Serializable { clip.start(); } - // while close() is not used because the same sounds are either constantly used or immediately dereferenced (and therefore collected by the GC) - // it is still good practice to enable closing sounds after finishing using them + // close sound after use public void close() { clip.close(); } diff --git a/src/UtilityFunction.java b/src/UtilityFunction.java index 62655b9..3c16ad9 100644 --- a/src/UtilityFunction.java +++ b/src/UtilityFunction.java @@ -1,6 +1,7 @@ // Eric Li, Charlie Zhao, ICS4U, Finished 6/20/2022 // Utility functions to help with common tasks +import javax.sound.sampled.LineEvent; import javax.sound.sampled.LineUnavailableException; import javax.sound.sampled.UnsupportedAudioFileException; import java.awt.event.KeyEvent; @@ -45,6 +46,12 @@ public final class UtilityFunction { // start playing a sound that is located at filePath public static void playSound(String filePath) throws UnsupportedAudioFileException, LineUnavailableException, IOException { Sound sound = new Sound(filePath); + sound.clip.addLineListener(e -> { + // close clip after sound is done playing + if (e.getType() == LineEvent.Type.STOP) { + sound.close(); + } + }); sound.start(); } }