1

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.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Zeeen
  • 312
  • 1
  • 2
  • 13
  • The question linked as a duplicate is asking about how to change the volume of a `Clip` that *is looping,* but it identifies the correct class, and shows how to gain a volume control for it. – Andrew Thompson May 04 '20 at 04:55
  • @AndrewThompson The clip isn't looping at all what do you mean? – Zeeen May 05 '20 at 01:25
  • @AndrewThompson and I asked how to edit my code so I can still use the same syntax but change the volume, what? – Zeeen May 05 '20 at 01:26
  • Unfortunately, you are going to have to change a fair bit to get your code to work, since support for Applets has been dropped. The link that Andrew provided shows how to use a `FloatControl` to control the volume of a `Clip`. I just added another answer with a link to a little library I wrote called `AudioCue` that has an improved volume control capabilities (real-time, per frame). Another option to look into would be the JavaFX `AudioClip` if you are using JavaFX in your code. Neat to see code by Notch! But in this instance he's just doing very standard stuff that is now deprecated. – Phil Freihofner May 08 '20 at 19:55

0 Answers0