[Python] python set and get windows 11 volume

Discussão em 'Python' iniciado por Stack, Novembro 30, 2024.

  1. Stack

    Stack Membro Participativo

    I have this script:

    from pycaw.pycaw import AudioUtilities, IAudioEndpointVolume
    from ctypes import cast, POINTER
    from comtypes import CLSCTX_ALL, CoInitialize, CoUninitialize
    CLSCTX_ALL = 7

    import time

    def set_windows_volume(value_max_100):
    devices = AudioUtilities.GetSpeakers()
    interface = devices.Activate(IAudioEndpointVolume._iid_, CLSCTX_ALL, None)
    volume = cast(interface, POINTER(IAudioEndpointVolume))
    scalarVolume = int(value_max_100) / 100
    volume.SetMasterVolumeLevelScalar(scalarVolume, None)

    def get_windows_volume():
    devices = AudioUtilities.GetSpeakers()
    interface = devices.Activate(IAudioEndpointVolume._iid_, CLSCTX_ALL, None)
    windows_volume = cast(interface, POINTER(IAudioEndpointVolume))
    volume_percentage = int(round(windows_volume.GetMasterVolumeLevelScalar() * 100))
    return volume_percentage

    for i in range(0,100):
    set_windows_volume(i)
    time.sleep(2)
    print(get_windows_volume())


    but sometimes it raises errors:

    Exception ignored in: <function _compointer_base.__del__ at 0x000002B538E96B90>
    Traceback (most recent call last):
    File "C:\Python\lib\site-packages\comtypes\_post_coinit\unknwn.py", line 426, in __del__
    self.Release()
    File "C:\Python\lib\site-packages\comtypes\_post_coinit\unknwn.py", line 559, in Release
    return self.__com_Release() # type: ignore
    ValueError: COM method call without VTable
    Exception ignored in: <function _compointer_base.__del__ at 0x000002B538E96B90>
    Traceback (most recent call last):
    File "C:\Python\lib\site-packages\comtypes\_post_coinit\unknwn.py", line 426, in __del__
    self.Release()
    File "C:\Python\lib\site-packages\comtypes\_post_coinit\unknwn.py", line 559, in Release
    return self.__com_Release() # type: ignore
    ValueError: COM method call without VTable
    Exception ignored in: <function _compointer_base.__del__ at 0x000002B538E96B90>
    Traceback (most recent call last):
    File "C:\Python\lib\site-packages\comtypes\_post_coinit\unknwn.py", line 426, in __del__
    self.Release()
    File "C:\Python\lib\site-packages\comtypes\_post_coinit\unknwn.py", line 559, in Release
    return self.__com_Release() # type: ignore
    ValueError: COM method call without VTable
    Exception ignored in: <function _compointer_base.__del__ at 0x000002B538E96B90>
    Traceback (most recent call last):
    File "C:\Python\lib\site-packages\comtypes\_post_coinit\unknwn.py", line 426, in __del__
    self.Release()
    File "C:\Python\lib\site-packages\comtypes\_post_coinit\unknwn.py", line 559, in Release
    return self.__com_Release() # type: ignore
    ValueError: COM method call without VTable
    Exception ignored in: <function _compointer_base.__del__ at 0x000002B538E96B90>
    Traceback (most recent call last):
    File "C:\Python\lib\site-packages\comtypes\_post_coinit\unknwn.py", line 426, in __del__
    self.Release()
    File "C:\Python\lib\site-packages\comtypes\_post_coinit\unknwn.py", line 559, in Release
    return self.__com_Release() # type: ignore
    ValueError: COM method call without VTable
    Exception ignored in: <function _compointer_base.__del__ at 0x000002B538E96B90>
    Traceback (most recent call last):
    File "C:\Python\lib\site-packages\comtypes\_post_coinit\unknwn.py", line 426, in __del__
    self.Release()
    File "C:\Python\lib\site-packages\comtypes\_post_coinit\unknwn.py", line 559, in Release
    return self.__com_Release() # type: ignore
    ValueError: COM method call without VTable
    Exception ignored in: <function _compointer_base.__del__ at 0x000002B538E96B90>
    Traceback (most recent call last):
    File "C:\Python\lib\site-packages\comtypes\_post_coinit\unknwn.py", line 426, in __del__
    self.Release()
    File "C:\Python\lib\site-packages\comtypes\_post_coinit\unknwn.py", line 559, in Release
    return self.__com_Release() # type: ignore
    ValueError: COM method call without VTable
    Exception ignored in: <function _compointer_base.__del__ at 0x000002B538E96B90>
    Traceback (most recent call last):
    File "C:\Python\lib\site-packages\comtypes\_post_coinit\unknwn.py", line 426, in __del__
    self.Release()
    File "C:\Python\lib\site-packages\comtypes\_post_coinit\unknwn.py", line 559, in Release
    return self.__com_Release() # type: ignore
    ValueError: COM method call without VTable
    Exception ignored in: <function _compointer_base.__del__ at 0x000002B538E96B90>
    Traceback (most recent call last):
    File "C:\Python\lib\site-packages\comtypes\_post_coinit\unknwn.py", line 426, in __del__
    self.Release()
    File "C:\Python\lib\site-packages\comtypes\_post_coinit\unknwn.py", line 559, in Release
    return self.__com_Release() # type: ignore
    ValueError: COM method call without VTable
    Exception ignored in: <function _compointer_base.__del__ at 0x000002B538E96B90>
    Traceback (most recent call last):
    File "C:\Python\lib\site-packages\comtypes\_post_coinit\unknwn.py", line 426, in __del__
    self.Release()
    File "C:\Python\lib\site-packages\comtypes\_post_coinit\unknwn.py", line 559, in Release
    return self.__com_Release() # type: ignore
    ValueError: COM method call without VTable
    ======== Running on http://192.168.1.188:8080 ========
    Exception ignored in: <function _compointer_base.__del__ at 0x000002B538E96B90>
    Traceback (most recent call last):
    File "C:\Python\lib\site-packages\comtypes\_post_coinit\unknwn.py", line 426, in __del__
    self.Release()
    File "C:\Python\lib\site-packages\comtypes\_post_coinit\unknwn.py", line 559, in Release
    return self.__com_Release() # type: ignore
    ValueError: COM method call without VTable


    Basically i use this script in a multiprocessing.Process with CoInitialize and CoUninitialize and safe release, but the error is still there.

    Any help or alternatives?

    Continue reading...

Compartilhe esta Página