簡體   English   中英

在java游戲中使用聲音效果

[英]Using Sound Effects in a java game

基本上我試圖在一個簡單的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字節/幀,不支持。 at com.sun.media.sound.DirectAudioDevice $ DirectDL.implOpen(DirectAudioDevice.java:494)at com.sun.media.sound.DirectAudioDevice $ DirectClip.implOpen(DirectAudioDevice.java:1280)at com.sun.media.sound .AbstractDataLine.open(AbstractDataLine.java:107)at com.sun.media.sound.DirectAudioDevice $ DirectClip.open(DirectAudioDevice.java:1061)at com.sun.media.sound.DirectAudioDevice $ DirectClip.open(DirectAudioDevice.java) :1111)在SoundEffect。(SoundEffect.java:39)在SoundEffect。(SoundEffect.java:15)在Reward.react(Reward.java:41)at city.soi.platform.World.despatchCollisionEvents(World.java:425) )at city.soi.platform.World.step(World.java:608)at city.soi.platform.World.access $ 000(World.java:42)at city.soi.platform.World $ 1.actionPerformed(World。 java:756)at javax.swing.Timer.fireActionPerformed(Timer.java:271)at javax.swing.Timer $ DoPostEvent.run(Timer.java:201)at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java) :209)java的java.awt.EventQueue.dispatchEvent(EventQueue.java:597) 位於java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)的java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)位於java.awt.EventDispatchThread的java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)。 java.awt.EventDispatchThread.run上的java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)中的pumpEvents(EventDispatchThread.java:169)(EventDispatchThread.java:122)線程“AWT-EventQueue-0”java中的異常。 Reward.react(Reward.java:41)中的lang.ExceptionInInitializerError位於city.soi.platform.World.despatchCollisionEvents(World.java:425),位於city.soi.platform.World.step(World.java:608) .soi.platform.World.access $ 000(World.java:42)at city.soi.platform.World $ 1.actionPerformed(World.java:756)at javax.swing.Timer.fireActionPerformed(Timer.java:271)at java.awt.EventQueue.dis上的java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)中的javax.swing.Timer $ DoPostEvent.run(Timer.java:201) java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:269)java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java)中的patchEvent(EventQueue.java:597) :174)java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)引起的:位於SoundEffect的javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:1128)的com.sun.media.sound.WaveFileReader.getAudioInputStream(WaveFileReader.java:180)中的java.lang.NullPointerException。(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

發生這種情況是因為並非所有聲音驅動程序都支持所有比特率或編碼,或者因為機器沒有聲卡。 聲音可以用A lawmu law (或其他,如MP3)編碼,並且可以以不同的比特率和樣本大小進行編碼。 Java並不總是支持所有可能的聲音格式,具體取決於基本系統支持的內容。

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

  • 確保運行該程序的計算機具有合理的聲卡。 大多數時候,當我看到上面的例外情況時,我正在服務器機器上運行,沒有內置聲卡或聲卡非常蹩腳或沒有音頻路徑。
  • 如果您正在使用遠程終端或類似的東西,請確保聲音具有配置的播放方式。
  • 嘗試以不同的速率重新編碼聲音樣本,或者嘗試作為WAV文件,然后嘗試播放它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM