frgnca / AudioDeviceCmdlets

AudioDeviceCmdlets is a suite of PowerShell Cmdlets to control audio devices on Windows
MIT License
737 stars 92 forks source link

How to check Mute-State of non-default-device #58

Closed tomm1996 closed 2 years ago

tomm1996 commented 2 years ago

Hi, First of all, thanks for the handy cmdlet.

I hope this is the right place to ask:

I'm by no means a powershell expert (or even decent for that matter) but I'm currently trying to use your tool to switch from Input Device A to Input Device B if Device B is not muted. Else, I want to switch back.

This is what I currently have:

$Microphone = "{0.0.1.00000000}.{50486949-aae2-4574-97fb-4660fd26b6f7}"
$Headset = "{0.0.1.00000000}.{4bf15a7e-d091-4bcb-ae8f-5cadfdcffca7}"

while (1) {
    $CurrentDevice = Get-AudioDevice -Recording

    if ($currentDevice."ID" -eq $Microphone) {
        Set-AudioDevice -ID $Headset

        if (Get-AudioDevice -RecordingMute) {
            Set-AudioDevice -ID $Microphone
        } else {}
    }
    if ($currentDevice."ID" -eq $Headset) {
        if (Get-AudioDevice -RecordingMute) {
            Set-AudioDevice -ID $Microphone
            Write-Output "going back to Microphone"
        }
    }

    sleep 3;
}

But the switching basically cuts my voice for some time every three seconds, so it's pretty impractical. Is there a way to either do this event-based instead of in a loop or by checking the mute state of device B without switching? Or maybe an entirely different approach?

tomm1996 commented 2 years ago

I ended up solving it like this:

$Microphone = "{0.0.1.00000000}.{50486949-aae2-4574-97fb-4660fd26b6f7}"
$Headset = "{0.0.1.00000000}.{4bf15a7e-d091-4bcb-ae8f-5cadfdcffca7}"

while (1) {
    $ActiveDevice = Get-AudioDevice -Recording
    $HeadSetObject = Get-AudioDevice -ID $Headset
    $HeadSetIsMuted = $HeadSetObject."Device"."AudioEndpointVolume"."Mute"
    if ($HeadSetIsMuted) {
        if ($ActiveDevice."ID" -eq $HeadSet) {
            Set-AudioDevice -ID $Microphone
        }
    } else {
        if ($ActiveDevice."ID" -eq $Microphone) {
            Set-AudioDevice -ID $Headset
        }
    }

    sleep 3;
}

This is device dependant though. $HeadSetObject."Device"."AudioEndpointVolume"."Mute" changes for the physical mute of my headset, but not for the physical mute of my microphone. You gotta test it out if you want to use it.