Merge remote-tracking branch 'origin/master'

master
Chara1236 2022-06-20 21:18:38 -04:00
commit a2aa091af0
4 changed files with 17 additions and 2 deletions

BIN
sound/pen.wav Normal file

Binary file not shown.

View File

@ -2,7 +2,10 @@
// displays dialogue, animates dialogue, and renders box that contains dialogue as well as the portraits of the characters // displays dialogue, animates dialogue, and renders box that contains dialogue as well as the portraits of the characters
// that speak to the players // that speak to the players
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import java.awt.*; import java.awt.*;
import java.io.IOException;
import java.io.Serializable; import java.io.Serializable;
import java.util.ArrayList; import java.util.ArrayList;
@ -101,6 +104,12 @@ public class DialogueMenu extends TextBox implements Serializable {
if (frameCounter >= FREQUENCY) { if (frameCounter >= FREQUENCY) {
frameCounter -= FREQUENCY; frameCounter -= FREQUENCY;
currentFrame += 1; 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 // set font of string to be drawn
g.setFont(font); g.setFont(font);

View File

@ -22,8 +22,7 @@ public class Sound implements Serializable {
clip.start(); clip.start();
} }
// while close() is not used because the same sounds are either constantly used or immediately dereferenced (and therefore collected by the GC) // close sound after use
// it is still good practice to enable closing sounds after finishing using them
public void close() { public void close() {
clip.close(); clip.close();
} }

View File

@ -1,6 +1,7 @@
// Eric Li, Charlie Zhao, ICS4U, Finished 6/20/2022 // Eric Li, Charlie Zhao, ICS4U, Finished 6/20/2022
// Utility functions to help with common tasks // Utility functions to help with common tasks
import javax.sound.sampled.LineEvent;
import javax.sound.sampled.LineUnavailableException; import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException; import javax.sound.sampled.UnsupportedAudioFileException;
import java.awt.event.KeyEvent; import java.awt.event.KeyEvent;
@ -45,6 +46,12 @@ public final class UtilityFunction {
// start playing a sound that is located at filePath // start playing a sound that is located at filePath
public static void playSound(String filePath) throws UnsupportedAudioFileException, LineUnavailableException, IOException { public static void playSound(String filePath) throws UnsupportedAudioFileException, LineUnavailableException, IOException {
Sound sound = new Sound(filePath); 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(); sound.start();
} }
} }