I have been trying to get this code to work for days now and I am honestly not sure how to resolve this error. The main goal of this code is to be able to control specific apps audio output instead of changing the whole computer volume. Below I have my code and the error I cannot fix, any help is appreciated.
My Code:
import ctypes
from ctypes import c_uint32, c_void_p, byref, Structure, POINTER, c_int, c_char
# Define constants
kAudioObjectSystemObject = 1
kAudioObjectPropertyScopeGlobal = 1
kAudioObjectPropertyElementMaster = 0
kAudioHardwarePropertyDevices = ctypes.c_uint32(1)
kAudioDevicePropertyDeviceNameCFString = ctypes.c_uint32(2)
# Define the AudioObjectPropertyAddress structure
class AudioObjectPropertyAddress(Structure):
_fields_ = [
('mSelector', c_uint32),
('mScope', c_uint32),
('mElement', c_uint32),
]
# Load CoreAudio framework
coreaudio = ctypes.CDLL('/System/Library/Frameworks/CoreAudio.framework/Versions/A/CoreAudio')
# OLD PATH: '/System/Library/Frameworks/CoreAudio.framework/CoreAudio'
# Define function prototypes
coreaudio.AudioObjectGetPropertyDataSize.argtypes = [c_uint32, POINTER(AudioObjectPropertyAddress), c_uint32, c_void_p, POINTER(c_uint32)]
coreaudio.AudioObjectGetPropertyDataSize.restype = c_int
coreaudio.AudioObjectGetPropertyData.argtypes = [c_uint32, POINTER(AudioObjectPropertyAddress), c_uint32, c_void_p, c_uint32, c_void_p]
coreaudio.AudioObjectGetPropertyData.restype = c_int
def list_audio_devices():
devices = []
# Define the property address for getting the list of devices
property_address = AudioObjectPropertyAddress(
mSelector=kAudioHardwarePropertyDevices.value,
mScope=kAudioObjectPropertyScopeGlobal,
mElement=kAudioObjectPropertyElementMaster
)
# Get the size of the property data
property_data_size = c_uint32(0)
status = coreaudio.AudioObjectGetPropertyDataSize(
kAudioObjectSystemObject,
byref(property_address),
0,
None,
byref(property_data_size)
)
if status != 0:
raise RuntimeError(f"AudioObjectGetPropertyDataSize failed with status {status}")
# Determine the number of devices
device_count = property_data_size.value // ctypes.sizeof(c_uint32)
# Create an array to hold the device IDs
device_ids = (c_uint32 * device_count)()
# Get the device IDs
status = coreaudio.AudioObjectGetPropertyData(
kAudioObjectSystemObject,
byref(property_address),
0,
None,
property_data_size.value,
byref(device_ids)
)
if status != 0:
raise RuntimeError(f"AudioObjectGetPropertyData failed with status {status}")
# Get the names of the devices
for device_id in device_ids:
property_address.mSelector = kAudioDevicePropertyDeviceNameCFString.value
property_data_size = c_uint32(0)
status = coreaudio.AudioObjectGetPropertyDataSize(
device_id,
byref(property_address),
0,
None,
byref(property_data_size)
)
if status != 0:
raise RuntimeError(f"AudioObjectGetPropertyDataSize failed for device {device_id} with status {status}")
# Create a buffer for the device name
device_name = ctypes.create_string_buffer(property_data_size.value)
status = coreaudio.AudioObjectGetPropertyData(
device_id,
byref(property_address),
0,
None,
property_data_size.value,
byref(device_name)
)
if status != 0:
raise RuntimeError(f"AudioObjectGetPropertyData failed for device {device_id} with status {status}")
devices.append({
'id': device_id,
'name': device_name.value.decode('utf-8')
})
return devices
# List all devices and their IDs
devices = list_audio_devices()
for device in devices:
print(f"Device ID: {device['id']}, Device Name: {device['name']}")
The Error:
Traceback (most recent call last):
File "/Users/MYNAME/Desktop/Core_Audio.py", line 112, in <module>
devices = list_audio_devices()
^^^^^^^^^^^^^^^^^^^^
File "/Users/MYNAME/Desktop/Core_Audio.py", line 53, in list_audio_devices
raise RuntimeError(f"AudioObjectGetPropertyDataSize failed with status {status}")
RuntimeError: AudioObjectGetPropertyDataSize failed with status 2003332927
I have tried renaming the file, uninstalling an reinstalling all imported files, ChatGPT, Perplexity. The main goal of this code is to just pull the audio sources from different apps so I can separately customize them within MacOS.
Mark Jabre is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.