You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python调用Apple Core Audio API报错(状态码2003332927)求助

MacOS应用级音频控制:CoreAudio代码报错求助

我尝试多日运行以下代码,目标是实现MacOS下特定应用的音频输出控制(而非调整全局系统音量),但始终无法解决报错问题。以下是我的代码、错误信息,以及已尝试的解决方式:

代码实现

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']}")

错误信息

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

已尝试的解决方式

  • 重命名代码文件
  • 卸载重装依赖库
  • 使用ChatGPT和Perplexity排查问题

核心需求

获取MacOS上不同应用的音频源,实现单独调整各应用的音频设置。

内容的提问来源于stack exchange,提问作者Mark

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 15:04:53