基本上我试图在一个简单的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
}
}
Run Code Online (Sandbox Code Playgroud)
以下是我的游戏奖励课程中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);
}
}
Run Code Online (Sandbox Code Playgroud)
当玩家与我的游戏中的奖励接触时,这应该播放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) )在city.soi.platform的city.soi.platform.World.step(World.java:608).
我无法弄清楚是什么错.我猜这与我的音频文件(WAV)不受支持有关.但是,无论我转换它们的方式有多少,它们仍然无法正常工作.
如果有人能够帮助我解决可能出错的问题以及如何解决这个问题,那将会非常有帮助.
我们将非常感谢您提供的示例代码和任何可以帮助您使代码正常工作的修改.
谢谢.
你的根本原因是例外
javax.sound.sampled.LineUnavailableException:
line with format ALAW 8000.0 Hz, 8 bit, stereo, 2 bytes/frame, not supported
Run Code Online (Sandbox Code Playgroud)
发生这种情况是因为并非所有声音驱动程序都支持所有比特率或编码,或者因为机器没有声卡.声音可以用A law或mu law(或其他,如MP3)编码,并且可以以不同的比特率和样本大小进行编码.Java并不总是支持所有可能的声音格式,具体取决于基本系统支持的内容.
您可能想要执行以下操作:
归档时间: |
|
查看次数: |
14091 次 |
最近记录: |