You can access the microphones through the Sound API, but this will not give you a simple volume level. You just need to capture the data and make your own decision about how loud it is.
http://download.oracle.com/javase/tutorial/sound/capturing.html
Recording involves saving data, but here you can drop the data as soon as you finish determining its volume.
Method The RMS method is a good way to calculate the amplitude of the wave data section.
In response to your comment, yes, you would capture a small data length (maybe just a few milliseconds) and calculate the amplitude of this. Repeat this periodically depending on how often you need updates. If you want to track previous volumes and compare them, then up to you - at the moment it's just a comparison of numbers. You can use the average of the latest volumes to calculate the external volume in the room so that you can detect a sharp increase in noise.
I don't know how much overhead it takes to turn audio capture on and off, but you might be better off keeping TargetDataLine open all the time and just calculating the volume when you need it. While the line is open, you need to continue to call read (), although otherwise the application freezes, waiting for you to read the data.
source share