I've been making a pong clone as a way of teaching myself java and tried to add sound effects to it. The problem? Only the very end of the sound clips were being played. I tried with 2 different files and the result was the same. But I also found that having audio playing on chrome while the game was running would cause both sound effects to play normally, and turning the background noise off would revert the fix. I have literally no idea what to do here. This is the code I have for the audio player is right here.
import java.io.File;
import javax.sound.sampled.*;
import java.io.IOException;
import java.URL;
public class AudioPlayer
{
boolean inQueue = false;
String filePath;
AudioInputStream audioInputStream;
Clip clip;
public AudioPlayer(String initialFilePath) {
filePath = initialFilePath;
try {
URL url = this.getClass().getClassLoader().getResource(filePath);
audioInputStream = AudioSystem.getAudioInputStream(url);
clip = AudioSystem.getClip();
clip.open(audioInputStream);
clip.start();
} catch (UnsupportedAudioFileException e) {
} catch (IOException e) {
} catch (LineUnavailableException e) {
}
}
public void play() {
if (clip != null) {
if (clip.isRunning())
clip.stop();
clip.flush();
clip.setFramePosition(0);
clip.start();
inQueue = false;
}
}
public void stop() {
if (clip != null) {
if (clip.isRunning()) {
clip.stop();
}
}
}
}
I have an Apple M2 processor by the way.