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.*;
public enum SoundEffect {
EAT("eat.wav"),
GONG("gong.wav"),
SHOOT("shoot.wav");
public static enum Volume {
MUTE, LOW, MEDIUM, HIGH
}
public static Volume volume = Volume.LOW;
private Clip clip;
SoundEffect(String soundFileName) {
try {
URL url = this.getClass().getClassLoader().getResource(soundFileName);
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(url);
clip = AudioSystem.getClip();
clip.open(audioInputStream);
} catch (UnsupportedAudioFileException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (LineUnavailableException e) {
e.printStackTrace();
}
}
public void play() {
if (volume != Volume.MUTE) {
if (clip.isRunning())
clip.stop();
clip.setFramePosition(0);
clip.start();
}
}
static void init() {
values();
}
}
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) . , , , .
, - , , .
, , , .
.
Dew