parent
7f5656a1a1
commit
41c5da9fb7
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue