问题
How do I change it?
I'm looking for something like:
SetMasterVolume(0.5)
SetAppVolume('FooBar',0.5)
I tried using ctypes.windll.winmm, but I can't find much documentation on how to use it.
Thanks in advance.
回答1:
I'd hope after 5 years this is no longer a problem for you, but I've just had to do the same thing. It's possible using the PyCaw library.
Simple proof of concept, based on PyCaw's examples
from __future__ import print_function
from pycaw.pycaw import AudioUtilities, ISimpleAudioVolume
def main():
sessions = AudioUtilities.GetAllSessions()
for session in sessions:
volume = session._ctl.QueryInterface(ISimpleAudioVolume)
if session.Process and session.Process.name() == "vlc.exe":
print("volume.GetMasterVolume(): %s" % volume.GetMasterVolume())
volume.SetMasterVolume(0.6, None)
if __name__ == "__main__":
main()
回答2:
I ripped this from here and modified it to use functions only.
import time import ctypes # Import the SendInput object SendInput = ctypes.windll.user32.SendInput # C struct redefinitions PUL = ctypes.POINTER(ctypes.c_ulong) class KeyBoardInput(ctypes.Structure): _fields_ = [ ("wVk", ctypes.c_ushort), ("wScan", ctypes.c_ushort), ("dwFlags", ctypes.c_ulong), ("time", ctypes.c_ulong), ("dwExtraInfo", PUL) ] class HardwareInput(ctypes.Structure): _fields_ = [ ("uMsg", ctypes.c_ulong), ("wParamL", ctypes.c_short), ("wParamH", ctypes.c_ushort) ] class MouseInput(ctypes.Structure): _fields_ = [ ("dx", ctypes.c_long), ("dy", ctypes.c_long), ("mouseData", ctypes.c_ulong), ("dwFlags", ctypes.c_ulong), ("time",ctypes.c_ulong), ("dwExtraInfo", PUL) ] class Input_I(ctypes.Union): _fields_ = [ ("ki", KeyBoardInput), ("mi", MouseInput), ("hi", HardwareInput) ] class Input(ctypes.Structure): _fields_ = [ ("type", ctypes.c_ulong), ("ii", Input_I) ] VK_VOLUME_MUTE = 0xAD VK_VOLUME_DOWN = 0xAE VK_VOLUME_UP = 0xAF def key_down(keyCode): extra = ctypes.c_ulong(0) ii_ = Input_I() ii_.ki = KeyBoardInput(keyCode, 0x48, 0, 0, ctypes.pointer(extra)) x = Input( ctypes.c_ulong(1), ii_ ) SendInput(1, ctypes.pointer(x), ctypes.sizeof(x)) def key_up(keyCode): extra = ctypes.c_ulong(0) ii_ = Input_I() ii_.ki = KeyBoardInput(keyCode, 0x48, 0x0002, 0, ctypes.pointer(extra)) x = Input(ctypes.c_ulong(1), ii_) SendInput(1, ctypes.pointer(x), ctypes.sizeof(x)) def key(key_code, length = 0): key_down(key_code) time.sleep(length) key_up(key_code) def volume_up(): key(VK_VOLUME_UP) def volume_down(): key(VK_VOLUME_DOWN) def set_volume(int): for _ in range(0, 50): volume_down() for _ in range(int / 2): volume_up()
回答3:
First import subprocess
import subprocess
Then to get the master volume
def get_master_volume(self):
proc = subprocess.Popen('/usr/bin/amixer sget Master', shell=True, stdout=subprocess.PIPE)
amixer_stdout = proc.communicate()[0].split('\n')[4]
proc.wait()
find_start = amixer_stdout.find('[') + 1
find_end = amixer_stdout.find('%]', find_start)
return float(amixer_stdout[find_start:find_end])
And to set the Master volume
def set_master_volume(self, widget):
val = self.volume
val = float(int(val))
proc = subprocess.Popen('/usr/bin/amixer sset Master ' + str(val) + '%', shell=True, stdout=subprocess.PIPE)
proc.wait()
回答4:
So instead of editing my old answer I'm adding a new post to allow others who use self to use my old code and anyone who's not, to use my new code.
def get_master_volume():
proc = subprocess.Popen('/usr/bin/amixer sget Master', shell=True, stdout=subprocess.PIPE)
amixer_stdout = proc.communicate()[0].split('\n')[4]
proc.wait()
find_start = amixer_stdout.find('[') + 1
find_end = amixer_stdout.find('%]', find_start)
return float(amixer_stdout[find_start:find_end])
def set_master_volume(volume):
val = float(int(volume))
proc = subprocess.Popen('/usr/bin/amixer sset Master ' + str(val) + '%', shell=True, stdout=subprocess.PIPE)
proc.wait()
来源:https://stackoverflow.com/questions/20828752/python-change-master-application-volume