Examples
This page provides examples of common Pycaw usage patterns.
All example source files are available in the examples/ directory of the repository.
Audio Endpoint Volume Control
Basic master volume control using the default speakers.
1"""
2Get and set access to master volume example.
3"""
4
5from pycaw.pycaw import AudioUtilities
6
7
8def main():
9 device = AudioUtilities.GetSpeakers()
10 print("Device found: %s" % device.FriendlyName)
11 volume = device.EndpointVolume
12 print("volume.GetMute(): %s" % volume.GetMute())
13 print("volume.GetMasterVolumeLevel(): %s" % volume.GetMasterVolumeLevel())
14 print("volume.GetVolumeRange(): (%s, %s, %s)" % volume.GetVolumeRange())
15 print("volume.SetMasterVolumeLevel()")
16 volume.SetMasterVolumeLevel(-20.0, None)
17 print("volume.GetMasterVolumeLevel(): %s" % volume.GetMasterVolumeLevel())
18
19
20if __name__ == "__main__":
21 main()
Volume Control by Process
Control volume for individual applications/processes.
1"""
2Mutes the volume of all processes, but unmutes chrome.exe process.
3"""
4
5from pycaw.pycaw import AudioUtilities
6
7
8def main():
9 sessions = AudioUtilities.GetAllSessions()
10 for session in sessions:
11 volume = session.SimpleAudioVolume
12 if session.Process and session.Process.name() == "chrome.exe":
13 volume.SetMute(0, None)
14 else:
15 volume.SetMute(1, None)
16
17
18if __name__ == "__main__":
19 main()
List and Switch Devices
Enumerate available audio devices and switch between them.
1"""
2Example to list and switch devices.
3"""
4
5import warnings
6
7from pycaw.constants import DEVICE_STATE, EDataFlow
8from pycaw.pycaw import AudioUtilities
9from pycaw.utils import AudioDevice
10
11
12def get_active_output_devices():
13 with warnings.catch_warnings(): # suppress COMError warnings
14 warnings.simplefilter("ignore", UserWarning)
15 return AudioUtilities.GetAllDevices(
16 data_flow=EDataFlow.eRender.value, device_state=DEVICE_STATE.ACTIVE.value
17 )
18
19
20def get_default_device():
21 return AudioUtilities.GetSpeakers()
22
23
24def set_default_device(device: AudioDevice):
25 AudioUtilities.SetDefaultDevice(device.id)
26
27
28if __name__ == "__main__":
29 # List devices
30 print("List of available output devices (* = default): ")
31 active_output_devices = get_active_output_devices()
32 default_device = get_default_device()
33 other_device = None
34 for device in active_output_devices:
35 if device.id == default_device.id:
36 print(f" * {device.FriendlyName}")
37 else:
38 print(f" {device.FriendlyName}")
39 other_device = device
40
41 if other_device is not None:
42 # Change default to other device
43 print(f"Changing default device to {other_device.FriendlyName}...")
44 set_default_device(other_device)
45
46 # List devices again
47 print("Updated list of available output devices (* = default): ")
48 active_output_devices = get_active_output_devices()
49 default_device = get_default_device()
50 for device in active_output_devices:
51 if device.id == default_device.id:
52 print(f"* {device.FriendlyName}")
53 else:
54 print(f" {device.FriendlyName}")
More Examples
Additional examples are available in the repository:
Session Callbacks:
session_callback_example.py- Event handling for audio session changesVolume Callbacks:
volume_callback_example.py- Event handling for volume changesNotification Client:
notification_client_example.py- Device change notificationsChannel Audio Volume:
channel_audio_volume_example.py- Per-channel volume controlSimple Audio Volume:
simple_audio_volume_example.py- Simple session volume controlMagic App Example:
magic_app_example.py- MTA COM initialization for notificationsAudio Controller Class:
audio_controller_class_example.py- Object-oriented wrapper example
You can view all examples on GitHub.