Patres
Patres

Reputation: 177

How to detect if a microphone is used by any application

I wrote a program that changes my light depending on whether I'm in a meeting or not. The easiest way to detect this is to check if the microphone is on. Currently, I check if a microphone icon appears on the screen (OpenCv): enter image description here

I'm sure it's not the most optimal solution. Is there any way in Java to detect the fact that a microphone is being used?

Upvotes: 1

Views: 1163

Answers (3)

Patres
Patres

Reputation: 177

Thanks to Eskandar Abedini I was able to do it! Below is the entire code:

public enum RegistryType {

    MICROPHONE("microphone"),
    WEBCAM("webcam");

    private final String pathValue;

    RegistryType(String pathValue) {
        this.pathValue = pathValue;
    }

    public String getNonPackagePath() {
        return "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\CapabilityAccessManager\\ConsentStore\\" + pathValue + "\\NonPackaged";
    }

}

public class RegistryManager {

    private static final String LAST_USED_TIME_STOP = "LastUsedTimeStop";
    private static final Long LAST_USED_TIME_STOP_VALUE = 0L;

    private RegistryManager() {
    }

    public static boolean isMicrophoneWorking() {
        return isDeviceWorking(MICROPHONE);
    }

    public static boolean isWebcamWorking() {
        return isDeviceWorking(WEBCAM);
    }
    private static boolean isDeviceWorking(final RegistryType registryType) {
        final String[] folders = Advapi32Util.registryGetKeys(WinReg.HKEY_CURRENT_USER, registryType.getNonPackagePath());
        return Arrays.stream(folders)
                .map(folder -> registryType.getNonPackagePath() + "\\" + folder)
                .map(register -> Advapi32Util.registryGetLongValue(WinReg.HKEY_CURRENT_USER, register, LAST_USED_TIME_STOP))
                .anyMatch(lastUsedTimeStop -> LAST_USED_TIME_STOP_VALUE.compareTo(lastUsedTimeStop) >= 0);
    }

}

And I also had to use: https://github.com/java-native-access/jna

Upvotes: 2

Eskandar Abedini
Eskandar Abedini

Reputation: 2154

You may check Windows registry, checking tray or other means for microphone usage

Detecting if mic/camera is in use?

Note:

Microsoft warns that desktop apps can record sound without ever notifying Windows. Because they're not subject to the sandbox restrictions of Microsoft Store apps, a desktop program can directly interface with your microphone hardware. This means malicious software could record without Windows being aware, so it won't show up in the list or display the microphone icon in the system tray.

Detecting if mic/camera is in use? How to see which apps are using your microphone in Windows 10

Upvotes: 0

Shterneregen
Shterneregen

Reputation: 71

A few years ago I was investigating how to transmit audio through the network, and I wrote simple ugly project.

And to check if a mic is ready to use I used:

javax.sound.sampled.TargetDataLine microphone = AudioSystem.getTargetDataLine(null);
if (!microphone.isOpen()) {
    // ...
}

Maybe it's a bit useless info but I hope it can be useful direction!

Link to class: MicrophoneReader.java

Upvotes: 1

Related Questions