Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java游戏中使用声音效果_Java_Audio_Effects - Fatal编程技术网

在java游戏中使用声音效果

在java游戏中使用声音效果,java,audio,effects,Java,Audio,Effects,基本上,我正试图在一个简单的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

基本上,我正试图在一个简单的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);
        }
    }
当玩家在我的游戏中接触到奖励时,应该会播放吃东西的声音

但是,当我的玩家与奖励冲突时,我在终端中会出现以下错误-

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) react(Reward.java:41)at city.soi.platform.World.DispatchCollisionEvents(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) 在 fireActionPerformed(Timer.java:271) 在 Timer$DoPostEvent.run(Timer.java:201) 在 java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209) 在 dispatchEvent(EventQueue.java:597) 在 java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269) 在 java.awt.EventDispatchThread.PumpeEventsforFilter(EventDispatchThread.java:184) 在 java.awt.EventDispatchThread.PumpeEventsforHierarchy(EventDispatchThread.java:174) 在 java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169) 在 java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161) 在 运行(EventDispatchThread.java:122) 线程“AWT-EventQueue-0”中出现异常 java.lang.ExceptionInInitializeError react(Reward.java:41)at city.soi.platform.World.DispatchCollisionEvents(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) 在 fireActionPerformed(Timer.java:271) 在 Timer$DoPostEvent.run(Timer.java:201) 在 java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209) 在 dispatchEvent(EventQueue.java:597) 在 java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269) 在 java.awt.EventDispatchThread.PumpeEventsforFilter(EventDispatchThread.java:184) 在 java.awt.EventDispatchThread.PumpeEventsforHierarchy(EventDispatchThread.java:174) 在 java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169) 在 java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161) 在 运行(EventDispatchThread.java:122) 原因: 位于的java.lang.NullPointerException com.sun.media.sound.waveileReader.getAudioInputStream(waveileReader.java:180) 在 javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:1128) 在 SoundEffect.(SoundEffect.java:35) 在 SoundEffect.(SoundEffect.java:16) ... 还有15个

我不知道怎么了。我猜这与我的音频文件(WAV)不受支持有关。然而,无论我用多少种方式转换它们,它们仍然不起作用

如果有人能善意地告诉我什么可能是错误的,以及我如何解决这个问题,那将是非常有帮助的

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


谢谢。

您的根本原因异常是

javax.sound.sampled.LineUnavailableException:
 line with format ALAW 8000.0 Hz, 8 bit, stereo, 2 bytes/frame, not supported
这是因为并非所有的声音驱动程序都支持所有的比特率或编码,或者因为机器没有声卡。声音可以用或(或其他,如MP3)编码,并且可以以不同的比特率和样本大小进行编码。Java并不总是支持所有可能的声音格式,这取决于基本系统支持什么

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

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