xcode 客观c音频表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3170002/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 19:30:28  来源:igfitidea点击:

objective c audio meter

objective-cxcodeaudio

提问by objectiveccoder001

Is it possible for xcode to have an audio level indicator?

xcode 是否可以具有音频电平指示器?

I want to do something like this:

我想做这样的事情:

if (audioLevel = 100) {
}

or something similar...

或类似的东西...

Any ideas?? Example code please?

有任何想法吗??请示例代码?

I'm VERY new to objective c so the more explaining the beter! :D

我对目标 c 非常陌生,所以解释得越多越好!:D

回答by Matt B.

Unfortunately, there isn't a very straightforward API to do this. You need to use the low level AudioToolbox.framework.

不幸的是,没有一个非常简单的 API 来做到这一点。您需要使用低级AudioToolbox.framework

Luckily, others have already solved this problem for you. Here's some code I simplified slightly to be straight C functions, from CocoaDev. You need to link to the AudioToolbox to compile this code (see here for documentation on how to do so).

幸运的是,其他人已经为您解决了这个问题。这是我从CocoaDev稍微简化为直接 C 函数的一些代码。您需要链接到 AudioToolbox 以编译此代码(有关如何执行此操作的文档,请参见此处)。

#import <AudioToolbox/AudioServices.h>

AudioDeviceID getDefaultOutputDeviceID()
{
    AudioDeviceID outputDeviceID = kAudioObjectUnknown;

    // get output device device
    OSStatus status = noErr;
    AudioObjectPropertyAddress propertyAOPA;
    propertyAOPA.mScope = kAudioObjectPropertyScopeGlobal;
    propertyAOPA.mElement = kAudioObjectPropertyElementMaster;
    propertyAOPA.mSelector = kAudioHardwarePropertyDefaultOutputDevice;

    if (!AudioHardwareServiceHasProperty(kAudioObjectSystemObject, &propertyAOPA))
    {
        printf("Cannot find default output device!");
        return outputDeviceID;
    }

    status = AudioHardwareServiceGetPropertyData(kAudioObjectSystemObject, &propertyAOPA, 0, NULL, (UInt32[]){sizeof(AudioDeviceID)}, &outputDeviceID);

    if (status != 0) 
    {
        printf("Cannot find default output device!");
    }
    return outputDeviceID;
}

float getVolume () 
{
    Float32 outputVolume;

    OSStatus status = noErr;
    AudioObjectPropertyAddress propertyAOPA;
    propertyAOPA.mElement = kAudioObjectPropertyElementMaster;
    propertyAOPA.mSelector = kAudioHardwareServiceDeviceProperty_VirtualMasterVolume;
    propertyAOPA.mScope = kAudioDevicePropertyScopeOutput;

    AudioDeviceID outputDeviceID = getDefaultOutputDeviceID();

    if (outputDeviceID == kAudioObjectUnknown)
    {
        printf("Unknown device");
        return 0.0;
    }

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

    status = AudioHardwareServiceGetPropertyData(outputDeviceID, &propertyAOPA, 0, NULL, (UInt32[]){sizeof(Float32)}, &outputVolume);

    if (status)
    {
        printf("No volume returned for device 0x%0x", outputDeviceID);
        return 0.0;
    }

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

    return outputVolume;
}

int main (int argc, char const *argv[])
{
    printf("%f", getVolume());
    return 0;
}

Note that there's also a setVolume function there, too.

请注意,那里也有一个 setVolume 函数。