I added the sound

This commit is contained in:
romca 2025-05-21 11:57:09 +02:00
parent 93e9c71855
commit 03c045380c
5 changed files with 61 additions and 0 deletions

2
OOP_1A2_Project/bin/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/backend/
/windowInterface/

Binary file not shown.

View File

@ -0,0 +1,59 @@
package backend;
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;
import java.io.File;
import java.io.IOException;
public class Sound {
private static Sound instance;
private Clip moveSound;
private static final String SOUND_FILE_PATH = "movesound.wav";
private Sound() {
loadSound();
}
public static Sound getInstance() {
if (instance == null) {
instance = new Sound();
}
return instance;
}
private void loadSound() {
try {
File soundFile = new File(SOUND_FILE_PATH);
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(soundFile);
moveSound = AudioSystem.getClip();
moveSound.open(audioInputStream);
} catch (UnsupportedAudioFileException | IOException | LineUnavailableException e) {
System.err.println("Error loading sound: " + SOUND_FILE_PATH + " - " + e.getMessage());
}
}
public void playMoveSound() {
if (moveSound != null) {
// Stop the clip if it's already playing
if (moveSound.isRunning()) {
moveSound.stop();
}
// Reset clip to beginning
moveSound.setFramePosition(0);
// Play the sound
moveSound.start();
}
}
public void cleanup() {
if (moveSound != null) {
moveSound.close();
}
}
}

Binary file not shown.

Binary file not shown.