AndreMiras / pycaw

Python Core Audio Windows Library
MIT License
385 stars 67 forks source link

Type object 'AudioUtilities' has no attribute 'GetMicrophone'. #58

Closed lastochka364 closed 1 year ago

lastochka364 commented 2 years ago

General Info:

  1. "pip freeze" output: cachetools==5.0.0 certifi==2021.10.8 charset-normalizer==2.0.10 comtypes==1.1.10 enum34==1.1.10 future==0.18.2 idna==3.3 pefile==2021.9.3 psutil==5.9.0 py2exe==0.11.0.1 pycaw==20181226 PyQt5==5.15.6 PyQt5-Qt5==5.15.2 PyQt5-sip==12.9.0 requests==2.27.1 urllib3==1.26.8
  2. OS: Microsoft Windows 21H2 LTSC 19044.1466. 64-bit.
  3. Python version: 3.10.

This message appears when installing pycaw using "pip install pycaw" and trying to get microphone: "AttributeError: type object 'AudioUtilities' has no attribute 'GetMicrophone'".

However, in dev version (pip install https://github.com/AndreMiras/pycaw/archive/develop.zip) error message is different. It's related on imports - when I'm trying to import AudioUtilities from pycaw.pycaw, this appears: from pycaw.api.audioclient import IAudioClient, ISimpleAudioVolume ModuleNotFoundError: No module named 'pycaw.api'.

The most interesting, that file "pycaw/utils.py", which now on github repo, has "GetMicrophone" method. Of course, I have already solved this - simply by pasting missing mehod in "Python\Lib\site-packages\pycaw.py" file, but that still a problem.

KillerBOSS2019 commented 2 years ago

One way to fix that is to download the api folder from https://github.com/AndreMiras/pycaw/tree/develop/pycaw and then put that folder in C:\Users\%USERNAME%\AppData\Local\Programs\Python\Python39\Lib\site-packages\pycaw

The reason why it gives you error says No module namedpycaw.api` is because in that pycaw folder there no folder called api

lastochka364 commented 1 year ago

Since more than year, I found that still opened. pycaw==20220416 correctly gives me access to method GetMicrophone():

from comtypes import CLSCTX_ALL, COMObject
from pycaw.pycaw import (AudioUtilities, IAudioEndpointVolume,
    IAudioEndpointVolumeCallback)

class MicrophoneController(AudioUtilities):
    '''
    This controller uses "pycaw" module to operate with microphone states.
    Initalizate connection to default microphone in system if any available.
    '''
    def __init__(self):
        self.__mic = cast(self.GetMicrophone().Activate(\
                IAudioEndpointVolume._iid_, CLSCTX_ALL, None),\
                    POINTER(IAudioEndpointVolume))

    def mute_mic(self):
        '''Mutes system microphone.'''
        self.__mic.SetMute(True, None)

    def unmute_mic(self):
        '''Unmutes system microphone.'''
        self.__mic.SetMute(False, None)
    ...

Thanks for such amazing project!