Skip to main content
Minor grammar fixes, markdown formatting
Source Link
liggiorgio
  • 5.5k
  • 6
  • 27
  • 38

control java Control sound (float control),volume in Java

IamI'm trying to control the volume of a file with keyinputkeyinput. But all I get is a null error:

edit: I tried using a value but is also gives me a null pointer. So I think there something different wrong.

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: Cannot invoke "javax.sound.sampled.FloatControl.setValue(float)" because "this.fc" is null

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: Cannot invoke "javax.sound.sampled.FloatControl.setValue(float)" because "this.fc" is null

I tried to set it to value (cVol)cVol but I also get aan error, so I thingthink the error is somewhere different.

HeresHere's my sound file. I want to change the volume with the 3 methods below. I used a debug text and the program arrived in the method. So the error is somewhere else. But I can't find it!!

public class Sound {

public class Sound {
    
    Clip clip;
    URL soundURL[] = new URL[30];
    float prevVol = 0;
    float cVol = 6.0f;
    boolean mute = false;
    FloatControl fc;
    
    public Sound() {
        
        soundURL[0] = getClass().getResource("/sound/BlueBoyAdventure.wav");
        soundURL[1] = getClass().getResource("/sound/coin.wav");

    }
    
    public void setFile(int i) {
        
        try {
            
            AudioInputStream ais = AudioSystem.getAudioInputStream(soundURL[i]);
            clip = AudioSystem.getClip();
            clip.open(ais);
            fc = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
            fc.setValue(cVol);
            
            
        }catch (Exception e) {
            
        }
    }
    public void volUp() {
        cVol += 1.0f;
        System.out.println("up");
        if(cVol > 6.0f) {
            cVol = 6.0f;
        }
        fc.setValue(cVol);
    }
    public void volDown() {
        cVol -= 1.0f;
        if(cVol > -80.0f) {
            cVol = -80.0f;
        }
        fc.setValue(cVol);  
    }
    public void mute() {
        if(mute == false) {
            prevVol = cVol;
            cVol = -80.0f;
            fc.setValue(cVol);
            mute = true;
        }else if(mute == true) {
            cVol = prevVol;
            fc.setValue(cVol);
            mute = false;
            
        }
    }
    
}

}

My keyHander. I want to control the volume with 0,9,8:


My keyHander. I want to control the volume with 0,9,8:

    if(code == KeyEvent.VK_8) {
            s.volUp();
            gp.ui.addMsg("volume up");
        }
        if(code == KeyEvent.VK_9) {
            s.volDown();
            gp.ui.addMsg("volume down");
        }
        if(code == KeyEvent.VK_0) {
            s.mute();
            gp.ui.addMsg("volume muted");
        }
```

control java sound (float control),

Iam trying to control the volume of a file with keyinput. But all I get is a null error:

edit: I tried using a value but is also gives me a null pointer. So I think there something different wrong.

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: Cannot invoke "javax.sound.sampled.FloatControl.setValue(float)" because "this.fc" is null

I tried to set it to value (cVol) but I also get a error so I thing the error is somewhere different.

Heres my sound file. I want to change the volume with the 3 methods below. I used a debug text and the program arrived in the method. So the error is somewhere else. But I can't find it!!

public class Sound {

Clip clip;
URL soundURL[] = new URL[30];
float prevVol = 0;
float cVol = 6.0f;
boolean mute = false;
FloatControl fc;

public Sound() {
    
    soundURL[0] = getClass().getResource("/sound/BlueBoyAdventure.wav");
    soundURL[1] = getClass().getResource("/sound/coin.wav");

}

public void setFile(int i) {
    
    try {
        
        AudioInputStream ais = AudioSystem.getAudioInputStream(soundURL[i]);
        clip = AudioSystem.getClip();
        clip.open(ais);
        fc = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
        fc.setValue(cVol);
        
        
    }catch (Exception e) {
        
    }
}
public void volUp() {
    cVol += 1.0f;
    System.out.println("up");
    if(cVol > 6.0f) {
        cVol = 6.0f;
    }
    fc.setValue(cVol);
}
public void volDown() {
    cVol -= 1.0f;
    if(cVol > -80.0f) {
        cVol = -80.0f;
    }
    fc.setValue(cVol);  
}
public void mute() {
    if(mute == false) {
        prevVol = cVol;
        cVol = -80.0f;
        fc.setValue(cVol);
        mute = true;
    }else if(mute == true) {
        cVol = prevVol;
        fc.setValue(cVol);
        mute = false;
        
    }
}

}

My keyHander. I want to control the volume with 0,9,8:

if(code == KeyEvent.VK_8) {
        s.volUp();
        gp.ui.addMsg("volume up");
    }
    if(code == KeyEvent.VK_9) {
        s.volDown();
        gp.ui.addMsg("volume down");
    }
    if(code == KeyEvent.VK_0) {
        s.mute();
        gp.ui.addMsg("volume muted");
    }

Control sound volume in Java

I'm trying to control the volume of a file with keyinput. But all I get is a null error:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: Cannot invoke "javax.sound.sampled.FloatControl.setValue(float)" because "this.fc" is null

I tried to set it to value cVol but I also get an error, so I think the error is somewhere different.

Here's my sound file. I want to change the volume with the 3 methods below. I used a debug text and the program arrived in the method. So the error is somewhere else. But I can't find it!

public class Sound {
    
    Clip clip;
    URL soundURL[] = new URL[30];
    float prevVol = 0;
    float cVol = 6.0f;
    boolean mute = false;
    FloatControl fc;
    
    public Sound() {
        
        soundURL[0] = getClass().getResource("/sound/BlueBoyAdventure.wav");
        soundURL[1] = getClass().getResource("/sound/coin.wav");

    }
    
    public void setFile(int i) {
        
        try {
            
            AudioInputStream ais = AudioSystem.getAudioInputStream(soundURL[i]);
            clip = AudioSystem.getClip();
            clip.open(ais);
            fc = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
            fc.setValue(cVol);
            
            
        }catch (Exception e) {
            
        }
    }
    public void volUp() {
        cVol += 1.0f;
        System.out.println("up");
        if(cVol > 6.0f) {
            cVol = 6.0f;
        }
        fc.setValue(cVol);
    }
    public void volDown() {
        cVol -= 1.0f;
        if(cVol > -80.0f) {
            cVol = -80.0f;
        }
        fc.setValue(cVol);  
    }
    public void mute() {
        if(mute == false) {
            prevVol = cVol;
            cVol = -80.0f;
            fc.setValue(cVol);
            mute = true;
        }else if(mute == true) {
            cVol = prevVol;
            fc.setValue(cVol);
            mute = false;
            
        }
    }
    
}

My keyHander. I want to control the volume with 0,9,8:

    if(code == KeyEvent.VK_8) {
            s.volUp();
            gp.ui.addMsg("volume up");
        }
        if(code == KeyEvent.VK_9) {
            s.volDown();
            gp.ui.addMsg("volume down");
        }
        if(code == KeyEvent.VK_0) {
            s.mute();
            gp.ui.addMsg("volume muted");
        }
```
added 112 characters in body
Source Link

edit: I tried using a value but is also gives me a null pointer. So I think there something different wrong.

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: Cannot invoke "javax.sound.sampled.FloatControl.setValue(float)" because "this.fc" is null

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: Cannot invoke "javax.sound.sampled.FloatControl.setValue(float)" because "this.fc" is null

edit: I tried using a value but is also gives me a null pointer. So I think there something different wrong.

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: Cannot invoke "javax.sound.sampled.FloatControl.setValue(float)" because "this.fc" is null

added 70 characters in body
Source Link

MyHeres my sound file. I want to change the volume inwith the 3 methods below. I used a debug text and the program arrived in the method. So the error is somewhere else. But I can't find it!!

My sound file. I change the volume in the 3 methods below. I used a debug text and the program arrived in the method.

Heres my sound file. I want to change the volume with the 3 methods below. I used a debug text and the program arrived in the method. So the error is somewhere else. But I can't find it!!

Source Link
Loading