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?

huangapple 未分类评论48阅读模式
英文:

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制作的非常旧的声音类。这是一个很好的声音类,唯一的问题是我无法更改其中音效的音量。我已经多次尝试修改它,但总是遇到非常令人困惑的错误和空指针异常。这是我的声音类:

  1. package com.teto.main;
  2. import java.applet.Applet;
  3. import java.applet.AudioClip;
  4. public class Sound {
  5. public static final Sound levelUp = new Sound("/example.wav");
  6. private AudioClip clip;
  7. private Sound(String name) {
  8. try {
  9. clip = Applet.newAudioClip(Sound.class.getResource(name));
  10. } catch (Throwable e) {
  11. e.printStackTrace();
  12. }
  13. }
  14. public void play() {
  15. try {
  16. new Thread() {
  17. public void run() {
  18. clip.play();
  19. }
  20. }.start();
  21. } catch (Throwable er) {
  22. er.printStackTrace();
  23. }
  24. }
  25. }

正如您所看到的,它使用了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:

  1. package com.teto.main;
  2. import java.applet.Applet;
  3. import java.applet.AudioClip;
  4. public class Sound {
  5. public static final Sound levelUp = new Sound("/example.wav");
  6. private AudioClip clip;
  7. private Sound(String name) {
  8. try {
  9. clip = Applet.newAudioClip(Sound.class.getResource(name));
  10. } catch (Throwable e) {
  11. e.printStackTrace();
  12. }
  13. }
  14. public void play() {
  15. try {
  16. new Thread() {
  17. public void run() {
  18. clip.play();
  19. }
  20. }.start();
  21. } catch (Throwable er) {
  22. er.printStackTrace();
  23. }
  24. }
  25. }

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.

huangapple
  • 本文由 发表于 2020年5月4日 03:42:26
  • 转载请务必保留本文链接:https://java.coder-hub.com/61580621.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定