Reputation: 61
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.
Upvotes: 6
Views: 6901
Reputation: 846
I improved Simon Flückiger answer with the help of Countour-Integral comment. This code just mutes the microphone by decreasing its volume 100 times. So calling it again wont toggle it on and off. You can easily turn it on by replacing APPCOMMAND_MICROPHONE_VOLUME_DOWN
with APPCOMMAND_MICROPHONE_VOLUME_UP
import win32api
import win32gui
WM_APPCOMMAND = 0x319
WM_BASE_HEX = 0xFFFF + 1
APPCOMMAND_MICROPHONE_VOLUME_DOWN = WM_BASE_HEX * 25
APPCOMMAND_MICROPHONE_VOLUME_UP = WM_BASE_HEX * 26
hwnd_active = win32gui.GetForegroundWindow()
for x in range(100):
win32api.SendMessage(hwnd_active, WM_APPCOMMAND, None, APPCOMMAND_MICROPHONE_VOLUME_DOWN)
# https://learn.microsoft.com/en-us/windows/win32/inputdev/wm-appcommand
Also here is a little python script to turn your mic on and off with a single key press. Can be useful in online meeting and games. Every MICROPHONE_VOLUME_DOWN
command decrease the volume by two so 50 call is actually enough.
import keyboard
import win32api
import win32gui
WM_APPCOMMAND = 0x319
WM_BASE_HEX = 0xFFFF + 1
APPCOMMAND_MICROPHONE_VOLUME_DOWN = WM_BASE_HEX * 25
APPCOMMAND_MICROPHONE_VOLUME_UP = WM_BASE_HEX * 26
def mute_mic(event):
hwnd_active = win32gui.GetForegroundWindow()
for x in range(50):
win32api.SendMessage(hwnd_active, WM_APPCOMMAND, None, APPCOMMAND_MICROPHONE_VOLUME_DOWN)
def unmute_mic(event):
hwnd_active = win32gui.GetForegroundWindow()
for x in range(50):
win32api.SendMessage(hwnd_active, WM_APPCOMMAND, None, APPCOMMAND_MICROPHONE_VOLUME_UP)
keyboard.on_press_key("m", mute_mic)
keyboard.on_press_key("u", unmute_mic)
keyboard.wait("q") # To quit the script
Upvotes: 1
Reputation: 21
import keyboard
import subprocess
p = subprocess.Popen(['C:\Windows\System32\mblctr.exe'])
keyboard.press_and_release('m')
p.kill()
You could use this method.
Upvotes: 2
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
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
Upvotes: 4
Reputation: 2309
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.
Upvotes: 1