I need to mute/unmute microphone on Windows 8 by python2.7. I find pyaudio and pymedia for interaction with sound devices, but can't find particular methods/realisations.
3 Answers
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
mute → unmute | unmute → mute
Here is a list of other useful parameters that can be used with WM_APPCOMMAND: WM_APPCOMMAND message (Winuser.h) - Win32 apps | Microsoft Docs
- 515
- 1
- 5
- 11
-
In the link listed, `APPCOMMAND_MICROPHONE_VOLUME_MUTE ` has a keycode (or whatever it's called) of `24` (decimal). The way `0x180000` is generated is by doing `(0xFFFF + 1) * 24`. So for all the keycodes there you must do `(0xFFFF + 1) * keycode` to get that number. For some reason the `win32` devs believe **mute** means **toggle**. This happens also with `APPCOMMAND_VOLUME_MUTE` which toggles sound instead of muting. There exists a `APPCOMMAND_MIC_ON_OFF_TOGGLE` which says **toggle** microphone but seems to do nothing despite the name. – Countour-Integral Jan 23 '21 at 15:04
A cursory look at the pymedia documentation confirms that finding this method is hard. Pymedia does not seem to be well documented. My suggestion, without knowing anything about the library, is to look at
Mixer(<Microphone Device ID>).getControls()
This supposedly returns a dictionary with the possible controls available to the device. However, you then need to figure out which one of those you want. Documentation implies a "Volume" and "Line In" entry should exist, both of which sound plausibly useful.
Then I suppose you have to poke around the 'controls' object within that dictionary and see what is available to you, possibly using reflection because the documentation is so lacking.
The final code might look something like this:
Mixer(<Microphone Device ID>).getControls()["Line In"].control.off()
(off() is not a actual method but something like it might exist)
Hope this helps.
EDIT: IMO this isn't a duplicate of How to toggle microphone on and off using python. That question does not actually answer anything, and instead just lists the libraries mentioned in the question. I feel like this question deserves some real code from someone more knowledgeable of the library in question.
- 1,780
- 5
- 29
- 35
import keyboard
import subprocess
p = subprocess.Popen(['C:\Windows\System32\mblctr.exe'])
keyboard.press_and_release('m')
p.kill()
You could use this method.
-
Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 22 '22 at 15:17