Mit Sound-Effekte in ein java-Spiel

Grundsätzlich bin ich versucht, zu verwenden diese SoundEffect-Klasse in ein einfaches java-Spiel, für das ich arbeite, für meine Aufgabe.

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
   }
}

Hier ist eine Implementierung des EAT-sound in meinem Spiel ist die Belohnung-Klasse-

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);
        }
    }

Diesen spielen sollte das ESSEN Klang beim player kommt in Kontakt mit einer Belohnung in meinem Spiel.

Aber wenn meine Spieler kollidiert mit einem Lohn, bekomme ich die folgenden Fehler in der terminal-

javax.sound.beprobt.LineUnavailableException:
Linie mit format ALAW 8000.0 Hz, 8
bit, stereo, 2 bytes/frame, nicht
unterstützt. bei
com.Sonne.media.sound.DirectAudioDevice$DirectDL.implOpen(DirectAudioDevice.java:494)
bei
com.Sonne.media.sound.DirectAudioDevice$DirectClip.implOpen(DirectAudioDevice.java:1280)
bei
com.Sonne.media.sound.AbstractDataLine.open(AbstractDataLine.java:107)
bei
com.Sonne.media.sound.DirectAudioDevice$DirectClip.open(DirectAudioDevice.java:1061)
bei
com.Sonne.media.sound.DirectAudioDevice$DirectClip.open(DirectAudioDevice.java:1151)
bei
SoundEffect.(SoundEffect.java:39)
bei
SoundEffect.(SoundEffect.java:15)
bei der Belohnung.reagieren(Belohnung.java:41) at
Stadt.soi.- Plattform.Welt.despatchCollisionEvents(Welt.java:425)
bei
Stadt.soi.- Plattform.Welt.Schritt(Welt.java:608)
bei
Stadt.soi.- Plattform.Welt.access$000(Welt.java:42)
bei
Stadt.soi.- Plattform.Welt$1.actionPerformed(Welt.java:756)
bei
javax.swing.Timer.fireActionPerformed(Timer).java:271)
bei
javax.swing.Timer$DoPostEvent.run(Timer.java:201)
bei
java.das awt.event.InvocationEvent.Versand(InvocationEvent.java:209)
bei
java.das awt.EventQueue.dispatchEvent(EventQueue.java:597)
bei
java.das awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
bei
java.das awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
bei
java.das awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
bei
java.das awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
bei
java.das awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
bei
java.das awt.EventDispatchThread.run(EventDispatchThread.java:122)
Exception in thread "AWT-EventQueue-0"
java.lang.ExceptionInInitializerError
bei der Belohnung.reagieren(Belohnung.java:41) at
Stadt.soi.- Plattform.Welt.despatchCollisionEvents(Welt.java:425)
bei
Stadt.soi.- Plattform.Welt.Schritt(Welt.java:608)
bei
Stadt.soi.- Plattform.Welt.access$000(Welt.java:42)
bei
Stadt.soi.- Plattform.Welt$1.actionPerformed(Welt.java:756)
bei
javax.swing.Timer.fireActionPerformed(Timer).java:271)
bei
javax.swing.Timer$DoPostEvent.run(Timer.java:201)
bei
java.das awt.event.InvocationEvent.Versand(InvocationEvent.java:209)
bei
java.das awt.EventQueue.dispatchEvent(EventQueue.java:597)
bei
java.das awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
bei
java.das awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
bei
java.das awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
bei
java.das awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
bei
java.das awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
bei
java.das awt.EventDispatchThread.run(EventDispatchThread.java:122)
Verursacht durch:
java.lang.NullPointerException bei
com.Sonne.media.sound.WaveFileReader.getAudioInputStream(WaveFileReader.java:180)
bei
javax.sound.beprobt.AudioSystem.getAudioInputStream(AudioSystem.java:1128)
bei
SoundEffect.(SoundEffect.java:35)
bei
SoundEffect.(SoundEffect.java:16)
... 15 mehr

Ich kann nicht herausfinden, was falsch ist. Ich vermute, es hat etwas zu tun mit meiner audio-Dateien (WAV) wird nicht unterstützt. Jedoch, egal wie viele Wege ich wandeln Sie Sie noch nicht arbeiten.

Wäre es wirklich hilfreich, wenn jemand freundlicherweise klärt mich auf, was falsch sein könnte und wie kann ich dieses Problem beheben.

Beispielcode und alle änderungen, die Sie anbieten können zu helfen, damit dieser code funktioniert, sind sehr willkommen.

Danke.

InformationsquelleAutor | 2009-03-15

Schreibe einen Kommentar