Audio-volume /w input Sound.sampled

lokmeinmatz

Neues Mitglied
Hey guys, I'm quite new to Java and wanted to access the Volume of the Mic-Sound for a
simple Audio-Visualizer. I got everything working except for my AudioInput class.
It doesn't throw exceptions or errors but the line.getlevel returns constantly -1.
If anyone has experience with that and could tell me what is wrong, it would be awesome.

My AudioInput class:
Java:
package audio;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.TargetDataLine;

public class audioInput {
    float sampleRate = 48000;
    int sampleSizeinBits = 16;
    int channels = 1;
    boolean signed = true;
    boolean bigE = true;
    AudioFormat format;
    DataLine.Info info;
    DataLine line;
    public audioInput(){
        format = new AudioFormat(sampleRate, sampleSizeinBits, channels, signed, bigE);
        info = new DataLine.Info(TargetDataLine.class, format);
        try {
            line = (TargetDataLine) AudioSystem.getLine(info);
        } catch (LineUnavailableException e) {
            e.printStackTrace();
        }
       
        try {
            line.open();
        } catch (LineUnavailableException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        line.start();
    }
   
   
    public float getVol(){
        return line.getLevel();
    }
}
 

Robat

Top Contributor
Hey lokmeinmatz,

Obtains the current volume level for the line. This level is a measure of the signal's current amplitude, and should not be confused with the current setting of a gain control. The range is from 0.0 (silence) to 1.0 (maximum possible amplitude for the sound waveform). The units measure linear amplitude, not decibels.

This is the Javadoc.

-1 is the value of the constant NOT_SPECIFIED. If you have a look at the documentation, you can read the following:
An integer that stands for an unknown numeric value. This value is appropriate only for signed quantities that do not normally take negative values.

I've never had any experiences with that but I guess that it returns -1 because you do not record anything?


BTW: You can put line = (...) ...; and line.open in one try-catch block :)
 

Ähnliche Java Themen

Neue Themen


Oben