Python检测win11扬声器是否正在发出声音
时间: 2024-03-14 13:44:37 浏览: 156
扬声器测试
你可以使用Python的`pycaw`库来检测Windows 11的扬声器是否正在发出声音。下面是一个简单的示例代码:
```python
import time
from pycaw.pycaw import AudioUtilities, ISimpleAudioVolume
def is_speaker_playing():
sessions = AudioUtilities.GetAllSessions()
for session in sessions:
volume = session._ctl.QueryInterface(ISimpleAudioVolume)
if volume.GetMasterVolume() > 0 and not session.IsSystemSoundsSession():
return True
return False
while True:
if is_speaker_playing():
print("Speaker is playing")
else:
print("Speaker is not playing")
time.sleep(1)
```
这段代码会每秒钟检查一次扬声器是否正在发出声音。如果是,则输出"Speaker is playing",否则输出"Speaker is not playing"。注意,这段代码只适用于Windows系统。
阅读全文