基本的に、私は自分の割り当てのために取り組んでいる単純な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)at com.sun.media.sound.DirectAudioDevice $ DirectClip.implOpen(DirectAudioDevice.java:1280)at com.sun.media.sound .AbstractDataLine.open(AbstractDataLine.java:107)at com.sun.media.sound.DirectAudioDevice $ DirectClip.open(DirectAudioDevice.java:1061)at com.sun.media.sound.DirectAudioDevice $ DirectClip.open(DirectAudioDevice.java :1151)at SoundEffect。(SoundEffect.java:39)at SoundEffect。(SoundEffect.java:15)at Reward.react(Reward.java:41)at city.soi.platform.World.despatchCollisionEvents(World.java:425 )city.soi.platform.World.step(World.java:608)のcity.soi.platform。
何が悪いのかわかりません。オーディオファイル(WAV)がサポートされていないことと関係があると思います。ただし、それらを変換する方法がいくつあっても、それらは機能しません。
誰かが私に何が間違っているのか、そして私がこれをどのように解決できるのかを親切に教えてくれると本当に助かります。
サンプルコードと、このコードを機能させるために提供できる変更をいただければ幸いです。
ありがとうございました。