使用Core Audio的音量滑块

时间:2015-01-30 00:16:52

标签: cocoa core-audio

我想在我的Mac应用中实现音量和增益滑块。我已经使用了将音量/增益设置为特定级别的代码,并试图将sample code与滑块一起使用,但音量/增益从不调整,即使我可以看到滑块的值传递给例程。那么使用Code Audio实现连续音量/增益水平的最佳方法是什么? 感谢

AudioObjectPropertyAddress      address;
    AudioDeviceID                   deviceID;
    OSStatus                        err;
    UInt32                          size;
    UInt32                          channels[ 2 ];
    Float32                         volume;

    SSAudioDevice * targetDevice = [self getTargetInputDevice];


    address.mSelector = kAudioHardwarePropertyDefaultInputDevice;
    address.mScope = kAudioObjectPropertyScopeGlobal;
    address.mElement = kAudioObjectPropertyElementMaster;
    deviceID = targetDevice.deviceID;


    if(!targetDevice.ioLevel){
            volume = 0.5;
    }
    else{
            volume = [targetDevice.ioLevel floatValue] / 100.0;
    }


    size = sizeof(deviceID);
    err = AudioObjectGetPropertyData( kAudioObjectSystemObject, &address, 0, nil, &size, &deviceID );


    if ( ! err ) {
            address.mSelector = kAudioDevicePropertyPreferredChannelsForStereo;
            address.mScope = kAudioDevicePropertyScopeInput;
            address.mElement = kAudioObjectPropertyElementWildcard;
            size = sizeof(channels);
            err = AudioObjectGetPropertyData( deviceID, &address, 0, nil, &size, &channels );
    }


    if ( ! err ) {
            Boolean hasProperty;

            address.mSelector = kAudioDevicePropertyVolumeScalar;
            address.mScope = kAudioDevicePropertyScopeInput;



            address.mElement = kAudioObjectPropertyElementMaster;

            hasProperty = AudioObjectHasProperty( deviceID, &address );
            if(!hasProperty){

            }
            address.mElement = channels[ 0 ];

            hasProperty = AudioObjectHasProperty( deviceID, &address );
            if(!hasProperty){

            }
            address.mElement = channels[ 1 ];
            // returns true, channel 1 has a VolumeScalar property
            hasProperty = AudioObjectHasProperty( deviceID, &address );
            if(!hasProperty){

            }

    }


    if ( ! err ) {
            address.mSelector = kAudioDevicePropertyVolumeScalar;
            address.mScope = kAudioDevicePropertyScopeInput;

            size = sizeof(volume);


            address.mElement = kAudioObjectPropertyElementMaster;

            err = AudioObjectSetPropertyData( deviceID, &address, 0, nil, size, &volume );

            if(err){
                    address.mElement = channels[ 0 ];

                    err = AudioObjectSetPropertyData( deviceID, &address, 0, nil, size, &volume );

                    address.mElement = channels[ 1 ];

                    err = AudioObjectSetPropertyData( deviceID, &address, 0, nil, size, &volume );
            }
    }

0 个答案:

没有答案