java - *.wav 文件需要位于目录中的什么位置?

标签 java wav javasound

我正在为游戏编写声音代码。我使用了以下代码:

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 开始,url 为 null,这告诉我 *.wav 文件本身没有被读取。

我已经尝试了以下几行,是的,*.wav 文件存在(我知道我不能让三个项目命名相同,我已经使用了一个玩过它,然后将其注释掉并用另一个重试一,我只是删除了“//”以使其更易于阅读):

TEST("file://C:/shoot.wav");
TEST("/soundTest/shoot.wav");
TEST("shoot.wav");  

此外,将文件的副本放置在包所在的目录(默认)、src 文件夹中,当然还有根目录 (c:) 中。

我如何调用枚举是在我的主语句中,它是所有标准 java 主代码,但具有:

 SoundEffect.SHOOT.play();

*.wav 文件到底需要位于目录中的什么位置?或者如果我遗漏了其他问题,请指出。我还在 Windows 8.1 上使用 Eclipse IDE“Kepler”。我想指出的是,发布的代码是我迄今为止所拥有的全部代码。

最佳答案

评论中有一些讨论认为 Eclipse 可能是问题所在。我严重怀疑 Eclipse 是问题所在。我已经使用 Eclipse 加载了数百个音频文件。通常我将文件放在调用代码下一级的子目录“audio”中,并使用相对地址形式:“audio/mySound.wav”。

以下是我加载 URL 的方法:

URL url = this.getClass().getResource("audio/" & fileName);

我很困惑为什么你的堆栈跟踪中有 MIDI 引用。 MIDI 与加载 .wav 文件无关,实际上根本不应该参与其中。您确定这是正确的堆栈跟踪吗?为什么我们会看到对 MIDI 的引用?

有时,无法识别的 .wav 文件格式会引发意外错误。最常见的 .wav 是 16 位编码、44100 bps、立体声、小端。您的 .wav 文件是这种格式吗?

您的代码的某些方面我还没有看到以这种方式实现,特别是 ENUMS 的使用。我会相信你的话,所有这些都已经过测试和验证。我倾向于只命名单个声音对象(使用带有 Clip 或 SourceDataLine 的 wav 文件包装器进行播放)并以这种方式使用它们。您所拥有的可能是一个很好的组织工具,但我不清楚它是否按预期工作。

例如,静态使用 SoundEffect,如 SoundEffect.SHOOT.play() 中,您确定它指向 shot.wav 吗?您是否编写了测试来验证这一点?将 ENUMS 和静态调用结合起来——这对我来说有点难以理解。

关于java - *.wav 文件需要位于目录中的什么位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21421970/

相关文章:

java - ResourceBundleMessageSource 中的嵌套属性

java - 在 Java 程序中调用时如何检索 .exe 文件的输出?

Java/OpenJDK : How to add JAR to classpath

python - 下采样 wav 音频文件

Java 从 MIDI 键盘获取输入

java - 这应该是 2 个 fragment 还是 2 个 Activity ?

java - 无法弄清楚为什么音频文件不播放 (Java)

c++ - 有没有办法标记一个 .wav 文件并在其中写入时间?

java - 如何从 .mp3 文件获取平移控制?

java - Java : What's the difference between javax. 声音和 sun.audio 中的声音?