diff --git a/src/SoundPlayer.java b/src/SoundPlayer.java new file mode 100644 index 0000000..1bb19c7 --- /dev/null +++ b/src/SoundPlayer.java @@ -0,0 +1,34 @@ +package impl; + +import javax.sound.sampled.*; +import java.io.File; +import java.io.IOException; + +public class SoundPlayer { + private Clip clip; + + public SoundPlayer(String filePath) { + try { + File soundFile = new File(filePath); + AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(soundFile); + clip = AudioSystem.getClip(); + clip.open(audioInputStream); + } catch (UnsupportedAudioFileException | IOException | LineUnavailableException e) { + e.printStackTrace(); + } + } + + public void play() { + if (clip != null) { + clip.setFramePosition(0); // Rewind to the beginning + clip.start(); + } + } + + public void stop() { + if (clip != null && clip.isRunning()) { + clip.stop(); + } + } + +} \ No newline at end of file