【问题标题】:CoreAudio findout if audio device can change volumeCoreAudio 找出音频设备是否可以改变音量
【发布时间】:2023-03-25 02:01:01
【问题描述】:

目前我正在寻找方法来确定音频设备是否可以配置音量。

我尝试使用 kAudioDevicePropertyVolumeRangeDecibels 属性来获取范围,如果最小值和最大值等于确认为不可更改的音量。但不幸的是,我根本无法获得那个价值。

AudioObjectPropertyAddress addr;
addr.mSelector = kAudioDevicePropertyVolumeRangeDecibels;
addr.mScope = kAudioDevicePropertyScopeOutput;
addr.mElement = kAudioDevicePropertyScopeOutput;

UInt32 size;
AudioValueRange range;

OSStatus status = AudioObjectGetPropertyDataSize(self.audioDeviceID, &addr, 0, NULL, &size);
if (status != noErr)
{
    NSLog(@"error during size retrieval");
}else {
    status = AudioObjectGetPropertyData(self.audioDeviceID, &addr, 0, NULL, &size, &range);
    if (status != noErr)
    {
        NSLog(@"error during value retrieval");
    }
}

在尺寸检索过程中我总是出错。 (所有其他数据,如音量、通道数等都被正确检索)。

感谢您提供任何解决方案。

【问题讨论】:

    标签: macos core-audio


    【解决方案1】:

    我通过检查设备是否支持kAudioDevicePropertyVolumeScalar 属性来实现这一点。一些设备(但不是很多)支持主通道,所以首先检查kAudioObjectPropertyElementMaster。如果失败,请检查您要使用的各个渠道:

    AudioObjectPropertyAddress propertyAddress = { 
        kAudioDevicePropertyVolumeScalar, 
        kAudioDevicePropertyScopeOutput,
        kAudioObjectPropertyElementMaster 
    };
    
    if(AudioObjectHasProperty(deviceID, &propertyAddress))
        // YES
    
    // Assume stereoChannels is a 2-deep array of the device's preferred stereo channels
    
    propertyAddress.mElement = stereoChannels[0];
    if(!AudioObjectHasProperty(deviceID, &propertyAddress))
        // NO
    
    propertyAddress.mElement = stereoChannels[1];
    if(!AudioObjectHasProperty(deviceID, &propertyAddress))
        // NO
    
    // YES
    

    【讨论】:

    • 非常感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 2013-04-03
    • 2021-11-14
    • 2012-03-29
    • 2021-11-19
    • 1970-01-01
    • 2015-05-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多