【问题标题】:Using Sound Effects in a java game在 Java 游戏中使用音效
【发布时间】:2009-03-15 03:43:39
【问题描述】:

基本上,我正在尝试在一个简单的 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.World.access$000(World.java:42) 在 city.soi.platform.World$1.actionPerformed(World.java:756) 在 javax.swing.Timer.fireActionPerformed(Timer.java:271) 在 javax.swing.Timer$DoPostEvent.run(Timer.java:201) 在 java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209) 在 java.awt.EventQueue.dispatchEvent(EventQueue.java:597) 在 java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269) 在 java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184) 在 java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174) 在 java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169) 在 java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161) 在 java.awt.EventDispatchThread.run(EventDispatchThread.java:122) 线程“AWT-EventQueue-0”中的异常 java.lang.ExceptionInInitializerError 在 Reward.react(Reward.java:41) 在 city.soi.platform.World.despatchCollisionEvents(World.java:425) 在 city.soi.platform.World.step(World.java:608) 在 city.soi.platform.World.access$000(World.java:42) 在 city.soi.platform.World$1.actionPerformed(World.java:756) 在 javax.swing.Timer.fireActionPerformed(Timer.java:271) 在 javax.swing.Timer$DoPostEvent.run(Timer.java:201) 在 java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209) 在 java.awt.EventQueue.dispatchEvent(EventQueue.java:597) 在 java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269) 在 java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184) 在 java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174) 在 java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169) 在 java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161) 在 java.awt.EventDispatchThread.run(EventDispatchThread.java:122) 造成的: java.lang.NullPointerException 在 com.sun.media.sound.WaveFileReader.getAudioInputStream(WaveFileReader.java:180) 在 javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:1128) 在 SoundEffect.(SoundEffect.java:35) 在 SoundEffect.(SoundEffect.java:16) ... 15 更多

我不知道哪里错了。我猜这与我的音频文件(WAV)不受支持有关。但是,无论我如何转换它们,它们仍然不起作用。

如果有人能告诉我可能出了什么问题以及如何解决这个问题,那将非常有帮助。

我们将非常感谢您提供示例代码和任何修改以帮助使该代码正常工作。

谢谢。

【问题讨论】:

    标签: java audio effects


    【解决方案1】:

    您的根本原因异常是

    javax.sound.sampled.LineUnavailableException:
     line with format ALAW 8000.0 Hz, 8 bit, stereo, 2 bytes/frame, not supported
    

    出现这种情况是因为并非所有声音驱动程序都支持所有比特率或编码,或者是因为机器没有声卡。声音可以以A lawmu law(或其他,例如MP3)编码,并且可以以不同的比特率和样本大小进行编码。 Java 并不总是支持所有可能的声音格式,具体取决于基本系统支持的内容。

    您可能想要执行以下操作:

    • 确保运行程序的计算机具有合理的声卡。大多数情况下,当我看到上述异常时,我是在没有内置声卡或声卡非常糟糕或没有音频路径的服务器机器上运行的。
    • 如果您使用远程终端或类似设备,请确保声音具有配置的播放方式。
    • 尝试以不同的速率重新编码您的声音样本,或者将其编码为 WAV 文件,然后尝试播放。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-19
      相关资源
      最近更新 更多