i am trying to write a program that detects when the headphones are plugged into the system and then run another piece of code when you remove the headphones. The code is working fine but the problem that i am facing is that the status of the device is getting cached so only 1 minute after i pull out the headphones does my program detect that it has been removed. This happens when i run the code is a endless while loop.
but if i run single instances not in a loop it shows the correct status immediately on the run
here is the code :
public class Headphonecheck {
public static int hpfound = 0;
public static int hpnfound =0;
public static void isHeadphoneAvailable() {
while (true)
{
if (!AudioSystem.isLineSupported(Port.Info.HEADPHONE) && hpnfound ==0) {
System.out.println("NO HEADPHONE FOUND");
System.out.println("Do something else");
hpfound = 0;
hpnfound = 1;
} else {
if(AudioSystem.isLineSupported(Port.Info.HEADPHONE) && hpfound ==0){
System.out.println("HEADPHONE FOUND");
System.out.println("Do something");
hpnfound = 0;
hpfound =1;
}
}
} //close while
}
public static void main (String[] args){
isHeadphoneAvailable();
}
}
if you remove the while loop and run the code it works perfectly for single run .. i.e. you plug in the headphones and run the code it shows HEADPHONE FOUND and then when you remove the headphones and run the code it shows NO HEADPHONE FOUND.
in the while loop once if you have the headphone in and run the code it shows HEADPHONE FOUND and then if you remove the headphone immediately it takes about 50 secs to show NO HEADPHONE FOUND. this is because it is caching it for 50 secs and then clearing the cache
HOW DO I STOP IT FROM CACHING OR CLEAR THE CACHE