0

私は10.8に取り組んでいます

これは、スピーカーの現在のボリュームを取得するコードです。

-(float)getVolume{

    float volume = 0.0;

    UInt32 thePropSize = sizeof(volume);

    AudioDeviceID devId = [self GetOutputAudioDevice];

    AudioObjectPropertyAddress thePropertyAddress = { kAudioDevicePropertyVolumeScalar, kAudioDevicePropertyScopeOutput, kAudioObjectPropertyElementMaster };


    if(AudioObjectHasProperty(devId, &thePropertyAddress)){
        AudioObjectGetPropertyData(devId, &thePropertyAddress, 0, NULL, &thePropSize, &volume);
    }else{
        printf(" doesn't have property to get the volume");
    }

    return volume;
}

Function AudioObjectHasProperty is Failing to get the Current Vol property 、何がうまくいかないのか、

これは、デフォルトの出力デバイスを選択するコードです。

-(AudioDeviceID)GetOutputAudioDevice{

    OSStatus err;
    AudioDeviceID device = 0;
    UInt32 size = sizeof(AudioDeviceID);
    AudioObjectPropertyAddress address = {
        kAudioHardwarePropertyDefaultOutputDevice,
        kAudioObjectPropertyScopeGlobal,
        kAudioObjectPropertyElementMaster
    };

    err = AudioObjectGetPropertyData(kAudioObjectSystemObject,
                                     &address,
                                     0,
                                     NULL,
                                     &size,
                                     &device);
    if (err)
    {
        NSLog(@"could not get default audio output device");
    }

    return device;
}
4

1 に答える 1

1

利用可能なオプションは 2 つあります。最初のステップは、必要なデバイスを決定し、その ID を取得することです。デフォルトの出力デバイスを想定すると、コードは次のようになります。

AudioObjectPropertyAddress propertyAddress = { 
    kAudioHardwarePropertyDefaultOutputDevice, 
    kAudioObjectPropertyScopeGlobal, 
    kAudioObjectPropertyElementMaster 
};

AudioDeviceID deviceID;
UInt32 dataSize = sizeof(deviceID);
OSStatus result = AudioObjectGetPropertyData(kAudioObjectSystemObject, &propertyAddress, 0, NULL, &dataSize, &deviceID);

if(kAudioHardwareNoError != result)
    // Handle the error
Next, you can use the kAudioHardwareServiceDeviceProperty_VirtualMasterVolume property to get the device's virtual master volume:

AudioObjectPropertyAddress propertyAddress = { 
    kAudioHardwareServiceDeviceProperty_VirtualMasterVolume, 
    kAudioDevicePropertyScopeOutput,
    kAudioObjectPropertyElementMaster 
};

if(!AudioHardwareServiceHasProperty(deviceID, &propertyAddress))
    // An error occurred

Float32 volume;
UInt32 dataSize = sizeof(volume);
OSStatus result = AudioHardwareServiceGetPropertyData(deviceID, &propertyAddress, 0, NULL, &dataSize, &volume);

if(kAudioHardwareNoError != result)
    // An error occurred
Alternatively, you can use kAudioDevicePropertyVolumeScalar to get the volume for a specific channel:

UInt32 channel = 1; // Channel 0  is master, if available
AudioObjectPropertyAddress propertyAddress = { 
    kAudioDevicePropertyVolumeScalar, 
    kAudioDevicePropertyScopeOutput,
    channel 
};

if(!AudioObjectHasProperty(deviceID, &propertyAddress))
    // An error occurred

Float32 volume;
UInt32 dataSize = sizeof(volume);
OSStatus result = AudioObjectGetPropertyData(deviceID, &propertyAddress, 0, NULL, &dataSize, &volume);

if(kAudioHardwareNoError != result)
    // An error occurred

2 つの違いは、Apple のドキュメントで説明されています。

kAudioHardwareServiceDeviceProperty_VirtualMasterVolume

音量コントロールの値を表す Float32 値。このプロパティの値の範囲は、0.0 (無音) から 1.0 (完全なレベル) です。このプロパティの効果は、HAL オーディオ オブジェクトに関連付けられているハードウェア デバイスによって異なります。デバイスにマスター ボリューム コントロールがある場合、このプロパティはそれを制御します。デバイスに個別のチャネル ボリューム コントロールがある場合、このプロパティは、デバイスの優先マルチチャネル レイアウトによって識別されるもの、またはデバイスがステレオのみの場合は優先ステレオ ペアに適用されます。このコントロールは、影響を与えるチャンネル間の相対的なバランスを維持します。

そのため、デバイスの音量を正確に定義するのは難しい場合があります。特に、非標準のチャンネル マップを備えたマルチチャンネル デバイスの場合はそうです。それが役立つことを願っています

于 2013-02-27T16:53:30.940 に答える