Q&A
前提
PythonでWin32APIを呼び出してデバイスの音量を取得したいです。
waveOutVolumeGet関数を呼び出して取得したいです。
デバイスのハンドルの取得はできていて、エラーも発生しなかったので問題ないと思うのですが、
結果が0しか返ってきませんでした。
ソースコード
Python
1import ctypes 2from ctypes import wintypes 3 4 5CALLBACK_NULL = 0x0000_0000 6 7class WAVEFORMATEX(ctypes.Structure): 8 _fields_ = [("wFormatTag", wintypes.WORD), 9 ("nChannels", wintypes.WORD), 10 ("nSamplesPerSec", wintypes.DWORD), 11 ("nAvgBytesPerSec", wintypes.DWORD), 12 ("nBlockAlign", wintypes.WORD), 13 ("wBitsPerSample", wintypes.WORD), 14 ("cbSize", wintypes.WORD)] 15 16 17def get_volume() -> int: 18 wave_format = WAVEFORMATEX() 19 wave_format.wFormatTag = 1 # WAVE_FORMAT_PCM 20 wave_format.nChannels = 2 21 wave_format.nSamplesPerSec = 44100 22 wave_format.nAvgBytesPerSec = 176400 23 wave_format.nBlockAlign = 4 24 wave_format.wBitsPerSample = 16 25 wave_format.cbSize = 0 26 27 winmm = ctypes.windll.winmm 28 waveOutOpen = winmm.waveOutOpen 29 waveOutOpen.argtypes = [wintypes.HANDLE, wintypes.UINT, WAVEFORMATEX, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_uint] 30 waveOutOpen.restype = ctypes.c_uint 31 32 device_handle = wintypes.HANDLE() 33 open_result = waveOutOpen(ctypes.byref(device_handle), wintypes.UINT(-1), wave_format, 0, 0, CALLBACK_NULL) 34 35 print("waveOutOpen errorCode", open_result) 36 print("outputDevice", device_handle) 37 38 vol = ctypes.c_uint() # この変数のポインタを指定して関数を実行 39 get_vol = winmm.waveOutGetVolume(device_handle, ctypes.byref(vol)) 40 print("waveOutGetVolume errorCode", get_vol) 41 return vol.value 42 43a = get_volume() 44print("\nresult", a)
試したこと
waveOutOpen関数の第二引数を違う値に変更
補足情報(FW/ツールのバージョンなど)
- Windows11 バージョン22H2
- Python 3.10.7
参考にしたドキュメント
回答1件
あなたの回答
tips
プレビュー
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
2023/01/21 02:14