lawre6b3
lawre6b3

Reputation: 89

How to toggle microphone on and off using python

I am wondering if there is a way to use python to mute and microphone? I am working on a project that requires the Microphone setting to be set to "Listen to this device". However, to prevent the Microphone from picking up unwanted noise from a TV or radio, I need a way to toggle between Mute and Unmute through a python script.

Upvotes: 5

Views: 8059

Answers (3)

python_learner55
python_learner55

Reputation: 1

the easiest solution to this, if you have Windows, is to download Windows PowerToys, run it as an administrator, turn on Video Conference mute, and VOILA, Win button + Shift + A will mute you.

There's an option to turn off your camera with a keyboard shortcut, too. And you can do both at the same time if you like.

Sometimes python isn't the right answer :)

Upvotes: 0

Simon Flückiger
Simon Flückiger

Reputation: 585

This can easily be achieved by using PyWin32:

import win32api
import win32gui

WM_APPCOMMAND = 0x319
APPCOMMAND_MICROPHONE_VOLUME_MUTE = 0x180000

hwnd_active = win32gui.GetForegroundWindow()
win32api.SendMessage(hwnd_active, WM_APPCOMMAND, None, APPCOMMAND_MICROPHONE_VOLUME_MUTE)

Unlike the name APPCOMMAND_MICROPHONE_VOLUME_MUTE would suggest, this actually toggles the mic

muteunmute | unmutemute

Here is a list of other useful parameters that can be used with WM_APPCOMMAND: WM_APPCOMMAND message (Winuser.h) - Win32 apps | Microsoft Docs

Upvotes: 6

Todd Knarr
Todd Knarr

Reputation: 1265

PyAudio is one cross-platform option for this. It's more than just direct access to the audio device controls, so it's moderately complicated to use. pymedia is another option, the pymedia.audio.sound package provides access to the mixer devices which is where the controls for the microphone (input level, mute etc.) would be.

Upvotes: 1

Related Questions