基本上我试图在我正在为我的任务工作的一个简单的java游戏中使用这个SoundEffect类。
import java.io.*;
import java.net.URL;
import javax.sound.sampled.*;
/**
* This enum encapsulates all the sound effects of a game, so as to separate the sound playing
* codes from the game codes.
* 1. Define all your sound effect names and the associated wave file.
* 2. To play a specific sound, simply invoke SoundEffect.SOUND_NAME.play().
* 3. You might optionally invoke the static method SoundEffect.init() to pre-load all the
* sound files, so that the play is not paused while loading the file for the first time.
* 4. You can use the static variable SoundEffect.volume to mute the sound.
*/
public enum SoundEffect {
EAT("eat.wav"), // explosion
GONG("gong.wav"), // gong
SHOOT("shoot.wav"); // bullet
// Nested class for specifying volume
public static enum Volume {
MUTE, LOW, MEDIUM, HIGH
}
public static Volume volume = Volume.LOW;
// Each sound effect has its own clip, loaded with its own sound file.
private Clip clip;
// Constructor to construct each element of the enum with its own sound file.
SoundEffect(String soundFileName) {
try {
// Use URL (instead of File) to read from disk and JAR.
URL url = this.getClass().getClassLoader().getResource(soundFileName);
// Set up an audio input stream piped from the sound file.
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(url);
// Get a clip resource.
clip = AudioSystem.getClip();
// Open audio clip and load samples from the audio input stream.
clip.open(audioInputStream);
} catch (UnsupportedAudioFileException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (LineUnavailableException e) {
e.printStackTrace();
}
}
// Play or Re-play the sound effect from the beginning, by rewinding.
public void play() {
if (volume != Volume.MUTE) {
if (clip.isRunning())
clip.stop(); // Stop the player if it is still running
clip.setFramePosition(0); // rewind to the beginning
clip.start(); // Start playing
}
}
// Optional static method to pre-load all the sound files.
static void init() {
values(); // calls the constructor for all the elements
}
}
这是我的游戏奖励类中 EAT 声音的实现-
public void react(CollisionEvent e)
{
Player player = game.getPlayer();
if (e.contact.involves(player)) {
player.changePlayerImageEAT();
SoundEffect.EAT.play();
player.addPoint();
System.out.println("Player now has: "+player.getPoints()+ " points.");
game.getCurrentLevel().getWorld().remove(this);
}
}
当玩家在我的游戏中接触到奖励时,这应该会播放 EAT 声音。
但是,当我的玩家与奖励发生碰撞时,我在终端中收到以下错误-
javax.sound.sampled.LineUnavailableException:行格式为 ALAW 8000.0 Hz,8 位,立体声,2 字节/帧,不支持。在 com.sun.media.sound.DirectAudioDevice$DirectDL.implOpen(DirectAudioDevice.java:494) 在 com.sun.media.sound.DirectAudioDevice$DirectClip.implOpen(DirectAudioDevice.java:1280) 在 com.sun.media.sound .AbstractDataLine.open(AbstractDataLine.java:107) 在 com.sun.media.sound.DirectAudioDevice$DirectClip.open(DirectAudioDevice.java:1061) 在 com.sun.media.sound.DirectAudioDevice$DirectClip.open(DirectAudioDevice.java :1151) 在 SoundEffect.(SoundEffect.java:39) 在 SoundEffect.(SoundEffect.java:15) 在 Reward.react(Reward.java:41) 在 city.soi.platform.World.despatchCollisionEvents(World.java:425) ) 在 city.soi.platform.World.step(World.java:608) 在 city.soi.platform。
我不知道出了什么问题。我猜这与我的音频文件(WAV)不受支持有关。但是,无论我转换它们的方式有多少,它们仍然不起作用。
如果有人能告诉我可能出了什么问题以及如何解决这个问题,那将非常有帮助。
示例代码和您可以提供以帮助使此代码正常工作的任何修改将不胜感激。
谢谢你。