0

Im trying to set the volume of a Clip in Java and Im fairly new to audio programming

Im following a previous post Set volume of Java Clip andswer by Steve but am having trouble using the method.

This is my working code just testing to play an audio file

try {
        AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File("C:\\Windows\\Media\\alarm01.wav").getAbsoluteFile());
        Clip clip = AudioSystem.getClip();
        //clip.setVolume(1.0f);
        clip.open(audioInputStream);
        clip.start();
    } catch(Exception ex) {
        System.out.println("Error with playing sound.");
        ex.printStackTrace();
    }

where the commented out line is the one that is producing the error. Below is my entire code in this class

public class Sound {

    /**
     * @param args the command line arguments
     */
    
    private static Clip clip;

    public float getVolume() {
        FloatControl gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
        return (float) Math.pow(10f, gainControl.getValue() / 20f);
    }

    public static void setVolume(float volume) {
        if (volume < 0f || volume > 1f) {
            throw new IllegalArgumentException("Volume not valid: " + volume);
        }
        FloatControl gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
        gainControl.setValue(20f * (float) Math.log10(volume));
    }
    
    public static void main(String[] args) throws Exception {
        int vol;
        Toolkit.getDefaultToolkit().beep();
        try {
            vol = Integer.parseInt(args[0]);
        }
        catch (NumberFormatException e) {
            vol = 0;
        }
        try {
        AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File("C:\\Windows\\Media\\alarm01.wav").getAbsoluteFile());
        Clip clip = AudioSystem.getClip();
        //clip.setVolume(1.0f);
        clip.open(audioInputStream);
        clip.start();
    } catch(Exception ex) {
        System.out.println("Error with playing sound.");
        ex.printStackTrace();
    }

The reason for the int vol in main is because that is whats meant to actual be the parameter to set the volume once I can get that method working

When I try to do the line

clip.setVolume(int);

it says it cannot find the symbol for the method setVolume and im not sure why

jquigs62
  • 19
  • 5

1 Answers1

0

The clip.setVolume() in your code is looking for a setVolume method on the class Clip.

The getVolume() that you created applies to the class Sound.

Phil Freihofner
  • 7,645
  • 1
  • 20
  • 41
  • So do you know how I would fix that then? I thought my setvolume class did apply to clip – jquigs62 Feb 16 '21 at 22:15
  • You have two distinct `Clip` instances: one that you create in the main() method and another that is declared as a static instance variable in the `Sound` class and is referenced in the Sound class methods. Use one or the other throughout! – Phil Freihofner Feb 16 '21 at 22:47
  • You might need to get a better grip on the basics of `static` and instance variables first. Might be good to review those concepts if they are new to you. They are basic/fundamental to Java. I can't tell from your post if you have them down or not. – Phil Freihofner Feb 16 '21 at 22:50
  • To be clear, if I remove the "clip." and just leave the line as setVolume when I call it, I get a nullpointer exception in that line and in the line that reads FloatControl gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN); – jquigs62 Feb 16 '21 at 23:05
  • Phil, just wanted to let you know that your comment helped me figure it out, I just had to create my own sound class that wasnt public. Sometimes I struggle with declaring methods and whether they should be public/static – jquigs62 Feb 16 '21 at 23:43
  • Glad to hear it! This is a tricky aspect of Java and it can take a while to get the hang of it. But it is really important to master it. I think that this is a prime situation where going through the exercise of working out the relationships and logic is much more valuable than copy/pasting a coded solution in. – Phil Freihofner Feb 17 '21 at 00:16