我正在研究遊戲的聲音代碼。我用的是下面的代碼:* .wav文件需要在目錄中存在的位置?
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 {
EXPLODE("explode.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
}
}
現在,當我更換從代碼中列出的* .wav文件之一,我自己還是連名字我自己從上面的代碼中列出的文件名之一。我收到以下錯誤:
Exception in thread "main" java.lang.ExceptionInInitializerError
at soundTest.main(soundTest.java:19)
Caused by: java.lang.NullPointerException
at com.sun.media.sound.StandardMidiFileReader.getSequence(Unknown Source)
at javax.sound.midi.MidiSystem.getSequence(Unknown Source)
at com.sun.media.sound.SoftMidiAudioFileReader.getAudioInputStream(Unknown Source)
at javax.sound.sampled.AudioSystem.getAudioInputStream(Unknown Source)
at SoundEffect.<init>(sfx.java:75)
at SoundEffect.<clinit>(sfx.java:55)
從以下堆棧URL網址爲空進去,這是告訴我的* .wav文件本身沒有被讀取。
我已經嘗試了以下幾行,並且是* .wav文件存在(我知道我不能有三個項目命名相同,我已經使用過一個玩過它,然後評論它,然後再試一次另外一個,我只是刪除了「//」,使更容易閱讀):
TEST("file://C:/shoot.wav");
TEST("/soundTest/shoot.wav");
TEST("shoot.wav");
除了,放置文件的副本中的目錄與包(默認),src文件夾中,當然還有根(c :)。
我如何envoking枚舉是我的主要發言,其中它是所有標準的Java,主要的代碼,但有:
SoundEffect.SHOOT.play();
到底在哪做的* .wav文件需要在詮釋他的目錄?或者如果有另一個問題,我錯過了,請指出。我也在Windows 8.1上使用Eclipse IDE「Kepler」。我想說明發布的代碼是迄今爲止我所擁有的。
根據您的示例,這些文件需要存儲在默認包中。話雖如此,我相信對於Eclipse,您需要在您的項目根目錄下創建一個「resources」文件夾,並將您希望包含在Jar中的資源放入其中。 – MadProgrammer
@MadProgrammer這樣糾正我,如果我錯了,我可以拖放文件,到package_Name,然後我只是像評論說的那樣調用它?因爲我已經嘗試過並且發生同樣的錯誤。 – user3247187
如果我正確理解Eclipse(我不是它的用戶),則需要在項目的頂層創建一個「resources」目錄。在這裏,你需要創建一個與SoundEffect的包結構相同的目錄結構。然後把你的文件放到這裏,乾淨並且構建......手指穿過 – MadProgrammer