admin管理员组文章数量:1356914
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.
本文标签: javaSound clips won39t fully playunless background noise is presentStack Overflow
版权声明:本文标题:java - Sound clips won't fully play, unless background noise is present - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744002021a2574033.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论