Objective C的音频表 [英] objective c audio meter

查看:271
本文介绍了Objective C的音频表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能对于x code有一个音频电平指示器?

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

我想要做这样的事情:

if (audioLevel = 100) {
}

或类似的东西...

任何想法?例如code吗?

Any ideas?? Example code please?

我很新的目标C所以越解释尤为明显! :D

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

推荐答案

不幸的是,还没有一个非常简单的API来做到这一点。你需要使用低电平<一个href=\"http://developer.apple.com/iphone/library/documentation/MusicAudio/Reference/CAAudioTooboxRef/index.html\">AudioToolbox.framework.

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

幸运的是,其他人已经解决了这个问题你。下面是一些code我微微简化为直C函数,从 CocoaDev 。您需要链接到AudioToolbox编译这个code(<一个href=\"http://developer.apple.com/mac/library/documentation/MacOSX/Conceptual/BPFrameworks/Tasks/IncludingFrameworks.html#//apple_ref/doc/uid/20002257-BAJJBBHJ\">see这里就如何做到这一点)文档。

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).

#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;
}

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

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

这篇关于Objective C的音频表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆