如何获得 Mac 音频电平?

Posted

技术标签:

【中文标题】如何获得 Mac 音频电平?【英文标题】:How to get Mac audio level? 【发布时间】:2014-12-14 04:25:32 【问题描述】:

目前,我的代码成功返回了用户可以使用音量键设置的系统音频值。

但是,我想要的是扬声器正在播放的音频的值。因此,如果用户正在观看 Netflix 并且角色开始尖叫,则返回的值将高于角色在耳语时的值。

我现在拥有的代码:

+ (AudioDeviceID)defaultOutputDeviceID 
    OSStatus status = noErr;

    AudioDeviceID outputDeviceID = kAudioObjectUnknown;

    AudioObjectPropertyAddress propertyAOPA;
    propertyAOPA.mElement = kAudioObjectPropertyElementMaster;
    propertyAOPA.mScope = kAudioObjectPropertyScopeGlobal;
    propertyAOPA.mSelector = kAudioHardwarePropertyDefaultSystemOutputDevice;

    UInt32 propertySize = sizeof(outputDeviceID);

    if (!AudioHardwareServiceHasProperty(kAudioObjectSystemObject, &propertyAOPA)) 
        NSLog(@"Cannot find default output device!");
        return outputDeviceID;
    

    status = AudioHardwareServiceGetPropertyData(kAudioObjectSystemObject, &propertyAOPA, 0, NULL, &propertySize, &outputDeviceID);

    if(status) 
        NSLog(@"Cannot find default output device!");
    

    return outputDeviceID;


+ (float)volume 
    OSStatus status = noErr;

    AudioDeviceID outputDeviceID = [[self class] defaultOutputDeviceID];

    if (outputDeviceID == kAudioObjectUnknown) 
        NSLog(@"Unknown device");
        return 0.0;
    

    AudioObjectPropertyAddress propertyAOPA;
    propertyAOPA.mElement = kAudioObjectPropertyElementMaster;
    propertyAOPA.mScope = kAudioDevicePropertyScopeOutput;
    propertyAOPA.mSelector = kAudioHardwareServiceDeviceProperty_VirtualMasterVolume;

    Float32 outputVolume;
    UInt32 propertySize = sizeof(outputVolume);

    if (!AudioHardwareServiceHasProperty(outputDeviceID, &propertyAOPA)) 
        NSLog(@"No volume returned for device 0x%0x", outputDeviceID);
        return 0.0;
    

    status = AudioHardwareServiceGetPropertyData(outputDeviceID, &propertyAOPA, 0, NULL, &propertySize, &outputVolume);

    if (status) 
        NSLog(@"No volume returned for device 0x%0x", outputDeviceID);
        return 0.0;
    

    if (outputVolume < 0.0 || outputVolume > 1.0)
        return 0.0;

    return outputVolume;

【问题讨论】:

How can I get the current sound level of the current audio output device?、this tutorial may also help. 的可能副本 不完全相同的问题。我正在寻找音频级别本身。不是系统音频电平。老实说,该答案中的代码对我没有多大帮助。 【参考方案1】:

获取音量级别并根据需要进行设置(作为最大音量),然后将其再次恢复为用户的原始音量级别。有关更多详细信息,您可以查看以下链接; https://***.com/a/27743599/1351327 希望它会有所帮助。 (注意 Apple 会拒绝您的应用)

【讨论】:

我只测试并在 i-phone 上使用它,所以我不确定它是否适用于 Mac。

以上是关于如何获得 Mac 音频电平?的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 Mac OS X 的音频 API 函数?

iPhone 音频工具箱中的音频电平表是啥?

从 Windows Phone 上的轨道检测音频电平

c# Naudio 音频电平捕获和显示,仅在打开录音属性时有效

音频:如何设置默认麦克风的电平?

Naudio - 计算音频电平上升到一个电平以上的次数