Using sound effects in a java game

I mainly try to use this SoundEffect class in a simple java game that I am working on for my assignment.

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

Here is the implementation of the EAT sound in my game. Reward Class -

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

This should play an EAT sound when a player comes in contact with an award in my game.

However, when my player encounters a reward, I get the following errors in the terminal -

javax.sound.sampled.LineUnavailableException: ALAW 8000,0 , 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) . , , , .

, - , , .

, , , .

.

+3
1

Exception

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

, , . mu law ( , MP3) . Java , , .

, :

  • , , , . , , .
  • - , , .
  • , , WAV, .
+5

Source: https://habr.com/ru/post/1704780/


All Articles