我写了这个音频类,我的声音播放器类使用它正常工作,没有错误,但当快速连续使用声音(即快速跳跃)时,我的游戏会打嗝或晃动。我认为通过加载一次并每次倒带(使用标记和重置方法),声音将保留在RAM中,但它似乎没有这样做。我对任何更改或完全重写是开放的。我只是需要它才能工作我已经被困在音频上好几个月了。
package com.bigdirty1985.squaresthatmove.sound;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;
public class Sound {
@SuppressWarnings("restriction")
private AudioStream audioStream;
private File file;
@SuppressWarnings("restriction")
public Sound(File file) {
this.file = file;
try {
this.audioStream = new AudioStream(new FileInputStream(this.file));
this.audioStream.mark(100000000);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@SuppressWarnings("restriction")
public void play() {
AudioPlayer.player.stop(this.audioStream);
AudioPlayer.player.start(this.audioStream);
try {
this.audioStream.reset();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@SuppressWarnings("restriction")
public void stop() {
AudioPlayer.player.stop(this.audioStream);
}
}