PyCaw Examples
PyCaw Tutorial: How To Examples in Python
PyCaw is a Python library that provides a high-level interface for manipulating audio devices on Windows. It allows you to control the volume, mute/unmute, and manage other audio settings programmatically. In this tutorial, we will cover various examples to demonstrate the usage of PyCaw.
Example 1: Get Default Audio Device
To get the default audio device, you can use the pycaw.pycaw.CAudioUtilities class. Here's an example:
from pycaw.pycaw import CAudioUtilities
devices = CAudioUtilities.GetSpeakers()
default_device = devices.Activate(CAudioUtilities.GetSpeakersIID())
print(default_device.FriendlyName())
Expected Output:
The example code will print the friendly name of the default audio device.
Example 2: Get a List of All Audio Devices
You can retrieve a list of all audio devices using the pypaw.pycaw.CAudioUtilities class. Here's an example:
from pycaw.pycaw import CAudioUtilities
devices = CAudioUtilities.GetSpeakers()
device_list = devices.GetAll()
for device in device_list:
print(device.FriendlyName())
Expected Output:
The example code will print the friendly names of all the audio devices on your system.
Example 3: Get and Set Volume
To get and set the volume level of an audio device, you can use the pypaw.pycaw.CAudioEndpointVolume class. Here's an example:
from pycaw.pycaw import AudioUtilities, ISimpleAudioVolume
def get_volume():
devices = AudioUtilities.GetSpeakers()
interface = devices.Activate(ISimpleAudioVolume._iid_, CLSCTX_ALL, None)
volume = cast(interface, POINTER(ISimpleAudioVolume))
return volume.GetMasterVolume()
def set_volume(volume_level):
devices = AudioUtilities.GetSpeakers()
interface = devices.Activate(ISimpleAudioVolume._iid_, CLSCTX_ALL, None)
volume = cast(interface, POINTER(ISimpleAudioVolume))
volume.SetMasterVolume(volume_level, None)
# Get the current volume level
current_volume = get_volume()
print("Current Volume:", current_volume)
# Set the volume level to 50%
set_volume(0.5)
Expected Output:
The example code will print the current volume level and set the volume to 50%.
Example 4: Mute and Unmute Audio
To mute and unmute an audio device, you can use the pypaw.pycaw.CAudioEndpointVolume class. Here's an example:
from pycaw.pycaw import AudioUtilities, ISimpleAudioVolume
def mute():
devices = AudioUtilities.GetSpeakers()
interface = devices.Activate(ISimpleAudioVolume._iid_, CLSCTX_ALL, None)
volume = cast(interface, POINTER(ISimpleAudioVolume))
volume.SetMute(1, None)
def unmute():
devices = AudioUtilities.GetSpeakers()
interface = devices.Activate(ISimpleAudioVolume._iid_, CLSCTX_ALL, None)
volume = cast(interface, POINTER(ISimpleAudioVolume))
volume.SetMute(0, None)
# Mute the audio
mute()
# Unmute the audio
unmute()
Expected Output:
The example code will mute and then unmute the audio.
Example 5: Get and Set Audio Session Volume
To get and set the volume level of an audio session, you can use the pypaw.pycaw.CAudioSessionControl2 class. Here's an example:
from pycaw.pycaw import AudioUtilities, ISimpleAudioVolume
def get_session_volume(session):
interface = session.QueryInterface(ISimpleAudioVolume)
return interface.GetMasterVolume()
def set_session_volume(session, volume_level):
interface = session.QueryInterface(ISimpleAudioVolume)
interface.SetMasterVolume(volume_level, None)
# Get the default audio device
devices = AudioUtilities.GetSpeakers()
default_device = devices.Activate(ISimpleAudioVolume._iid_, CLSCTX_ALL, None)
# Get the audio session for the default device
session_manager = default_device.QueryInterface(ISimpleAudioVolume)
sessions = session_manager.GetSessionEnumerator()
# Get the volume of the first session
session = sessions.GetSession(0)
session_volume = get_session_volume(session)
print("Session Volume:", session_volume)
# Set the volume of the first session to 50%
set_session_volume(session, 0.5)
Expected Output:
The example code will print the volume of the first audio session and set its volume to 50%.
Example 6: Get and Set Audio Session Mute
To get and set the mute state of an audio session, you can use the pypaw.pycaw.CAudioSessionControl2 class. Here's an example:
from pycaw.pycaw import AudioUtilities, ISimpleAudioVolume
def get_session_mute(session):
interface = session.QueryInterface(ISimpleAudioVolume)
return interface.GetMute()
def set_session_mute(session, mute_state):
interface = session.QueryInterface(ISimpleAudioVolume)
interface.SetMute(mute_state, None)
# Get the default audio device
devices = AudioUtilities.GetSpeakers()
default_device = devices.Activate(ISimpleAudioVolume._iid_, CLSCTX_ALL, None)
# Get the audio session for the default device
session_manager = default_device.QueryInterface(ISimpleAudioVolume)
sessions = session_manager.GetSessionEnumerator()
# Get the mute state of the first session
session = sessions.GetSession(0)
session_mute = get_session_mute(session)
print("Session Mute:", session_mute)
# Set the mute state of the first session to True
set_session_mute(session, True)
Expected Output:
The example code will print the mute state of the first audio session and set its mute state to True.
Example 7: Get Audio Session Information
To get information about an audio session, such as its process ID and display name, you can use the pypaw.pycaw.CAudioSessionControl class. Here's an example:
from pycaw.pycaw import AudioUtilities, ISimpleAudioVolume
def get_session_info(session):
interface = session.QueryInterface(ISimpleAudioVolume)
return {
"Process ID": session.GetProcessId(),
"Display Name": session.GetDisplayName(),
"Volume": interface.GetMasterVolume(),
"Mute": interface.GetMute()
}
# Get the default audio device
devices = AudioUtilities.GetSpeakers()
default_device = devices.Activate(ISimpleAudioVolume._iid_, CLSCTX_ALL, None)
# Get the audio session for the default device
session_manager = default_device.QueryInterface(ISimpleAudioVolume)
sessions = session_manager.GetSessionEnumerator()
# Get information about the first session
session = sessions.GetSession(0)
session_info = get_session_info(session)
print("Session Information:")
for key, value in session_info.items():
print(key + ":", value)
Expected Output:
The example code will print information about the first audio session, including its process ID, display name, volume, and mute state.
Example 8: Enumerate Audio Sessions
To enumerate all the audio sessions on the default audio device, you can use the pypaw.pycaw.CAudioSessionEnumerator class. Here's an example:
from pycaw.pycaw import AudioUtilities, ISimpleAudioVolume
# Get the default audio device
devices = AudioUtilities.GetSpeakers()
default_device = devices.Activate(ISimpleAudioVolume._iid_, CLSCTX_ALL, None)
# Get the audio session for the default device
session_manager = default_device.QueryInterface(ISimpleAudioVolume)
sessions = session_manager.GetSessionEnumerator()
# Enumerate all the sessions
for i in range(sessions.GetCount()):
session = sessions.GetSession(i)
print("Session", i + 1, "Process ID:", session.GetProcessId())
Expected Output:
The example code will print the process ID of each audio session on the default audio device.
Example 9: Set Volume for a Specific Audio Session
To set the volume level for a specific audio session, you can use the pypaw.pycaw.CAudioSessionControl class. Here's an example:
from pycaw.pycaw import AudioUtilities, ISimpleAudioVolume
def set_session_volume(session, volume_level):
interface = session.QueryInterface(ISimpleAudioVolume)
interface.SetMasterVolume(volume_level, None)
# Get the default audio device
devices = AudioUtilities.GetSpeakers()
default_device = devices.Activate(ISimpleAudioVolume._iid_, CLSCTX_ALL, None)
# Get the audio session for the default device
session_manager = default_device.QueryInterface(ISimpleAudioVolume)
sessions = session_manager.GetSessionEnumerator()
# Set the volume of the first session to 50%
session = sessions.GetSession(0)
set_session_volume(session, 0.5)
Expected Output:
The example code will set the volume of the first audio session on the default audio device to 50%.
Example 10: Get and Set Default Audio Device
To get and set the default audio device, you can use the pypaw.pycaw.CAudioUtilities class. Here's an example:
from pycaw.pycaw import AudioUtilities
def get_default_device():
devices = AudioUtilities.GetSpeakers()
return devices.GetDefault()
def set_default_device(device):
devices = AudioUtilities.GetSpeakers()
devices.SetDefault(device)
# Get the current default audio device
current_device = get_default_device()
print("Current Default Device:", current_device.FriendlyName())
# Set the default audio device to a different one
new_device = devices.GetAll()[1] # Assuming there are at least two devices
set_default_device(new_device)
Expected Output:
The example code will print the friendly name of the current default audio device and then set the default audio device to a different one.
In this tutorial, we covered various examples to demonstrate the usage of PyCaw for manipulating audio devices on Windows. You can now control the volume, mute/unmute, and manage other audio settings programmatically using Python.