3

基本上我试图在我正在为我的任务工作的一个简单的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)不受支持有关。但是,无论我转换它们的方式有多少,它们仍然不起作用。

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

示例代码和您可以提供以帮助使此代码正常工作的任何修改将不胜感激。

谢谢你。

4

1 回答 1

5

您的根本原因异常是

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

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

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

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