英文:
I have an old sound class and I want to change it from using applet.AudioClip to something modern so I can alter volume. How would I do this?
问题
我正在使用Java开发一款游戏,其中包含一些可爱的8位音效。我有一个来自2011年由Notch制作的非常旧的声音类。这是一个很好的声音类,唯一的问题是我无法更改其中音效的音量。我已经多次尝试修改它,但总是遇到非常令人困惑的错误和空指针异常。这是我的声音类:
package com.teto.main;
import java.applet.Applet;
import java.applet.AudioClip;
public class Sound {
public static final Sound levelUp = new Sound("/example.wav");
private AudioClip clip;
private Sound(String name) {
try {
clip = Applet.newAudioClip(Sound.class.getResource(name));
} catch (Throwable e) {
e.printStackTrace();
}
}
public void play() {
try {
new Thread() {
public void run() {
clip.play();
}
}.start();
} catch (Throwable er) {
er.printStackTrace();
}
}
}
正如您所看到的,它使用了java.applet.AudioClip
,我认为它不允许您更改与其一起使用的声音效果的音量。所以我想找出如何用更好的东西替换它,这样我仍然可以输入Sound.soundname.play();
,但我也可以更改音量。
英文:
I'm developing a game in java with some cutesy little 8-bit sfx and I have this incredibly old sound class from 2011 made by notch. It's a good sound class, the only problem is that I can not change the volume of the sounds in it, I have tried multiple times to alter this but I always get really confusing errors and NullPointerExceptions. This is my sound class:
package com.teto.main;
import java.applet.Applet;
import java.applet.AudioClip;
public class Sound {
public static final Sound levelUp = new Sound("/example.wav");
private AudioClip clip;
private Sound(String name) {
try {
clip = Applet.newAudioClip(Sound.class.getResource(name));
} catch (Throwable e) {
e.printStackTrace();
}
}
public void play() {
try {
new Thread() {
public void run() {
clip.play();
}
}.start();
} catch (Throwable er) {
er.printStackTrace();
}
}
}
As you can see it is using java.applet.AudioClip which I don't think allows you to alter the volume of the sound effects you use with it. So that's why I want to figure out how to replace this with something better, so that I can still type Sound.soundname.play(); but I can also alter the volume.
专注分享java语言的经验与见解,让所有开发者获益!
评论