2012-02-10 36 views
1

我正在尋找將NSSlider併入控制Mac主音量的Cocoa應用程序中。儘管我知道它可能更容易,但我不想通過AppleScript來做到這一點。通過Cocoa應用程序控制Mac上的主音量

我發現這個代碼應該能夠做到這一點,但並不是因爲自10.5 AudioDeviceGetProperty及其一致已被棄用。什麼需要改變,以使這項工作?

- (float) getVolume 
{ 
float   b_vol; 
OSStatus  err; 
AudioDeviceID  device; 
UInt32   size; 
UInt32   channels[2]; 
float   volume[2]; 

// get device 
size = sizeof device; 
err = AudioObjectGetPropertyData(kAudioHardwarePropertyDefaultOutputDevice, &device, <#UInt32 inQualifierDataSize#>, <#const void *inQualifierData#>, &size, <#void *outData#>); 
if(err!=noErr) 
{ 
    NSLog(@"audio-volume error get device"); 
    return 0.0; 
} 

// try set master volume (channel 0) 
size = sizeof b_vol; 
err = AudioDeviceGetProperty(device, 0, 0, kAudioDevicePropertyVolumeScalar, &size, &b_vol); //kAudioDevicePropertyVolumeScalarToDecibels 
if(noErr==err) return b_vol; 

// otherwise, try seperate channels 
// get channel numbers 
size = sizeof(channels); 
err = AudioDeviceGetProperty(device, 0, 0,kAudioDevicePropertyPreferredChannelsForStereo, &size,&channels); 
if(err!=noErr) NSLog(@"error getting channel-numbers"); 

size = sizeof(float); 
err = AudioDeviceGetProperty(device, channels[0], 0, kAudioDevicePropertyVolumeScalar, &size, &volume[0]); 
if(noErr!=err) NSLog(@"error getting volume of channel %d",channels[0]); 
err = AudioDeviceGetProperty(device, channels[1], 0, kAudioDevicePropertyVolumeScalar, &size, &volume[1]); 
if(noErr!=err) NSLog(@"error getting volume of channel %d",channels[1]); 

b_vol = (volume[0]+volume[1])/2.00; 

return b_vol; 
} 

- (void)setVolume:(float)involume { 
OSStatus  err; 
AudioDeviceID  device; 
UInt32   size; 
Boolean   canset = false; 
UInt32   channels[2]; 
//float   volume[2]; 

// get default device 
size = sizeof device; 
printf("setVolume:: value of the volume set =%lf", involume); 
err = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice, &size, &device); 
if(err!=noErr) { 
    NSLog(@"audio-volume error get device"); 
    return; 
} 


// try set master-channel (0) volume 
size = sizeof canset; 
err = AudioDeviceGetPropertyInfo(device, 0, false, kAudioDevicePropertyVolumeScalar, &size, &canset); 
if(err==noErr && canset==true) { 
    size = sizeof involume; 
    err = AudioDeviceSetProperty(device, NULL, 0, false, kAudioDevicePropertyVolumeScalar, size, &involume); 
    return; 
} 

// else, try seperate channes 
// get channels 
size = sizeof(channels); 
err = AudioDeviceGetProperty(device, 0, false, kAudioDevicePropertyPreferredChannelsForStereo, &size,&channels); 
if(err!=noErr) { 
    NSLog(@"error getting channel-numbers"); 
    return; 
} 

// set volume 
size = sizeof(float); 
err = AudioDeviceSetProperty(device, 0, channels[0], false, kAudioDevicePropertyVolumeScalar, size, &involume); 
if(noErr!=err) NSLog(@"error setting volume of channel %d",channels[0]); 
err = AudioDeviceSetProperty(device, 0, channels[1], false, kAudioDevicePropertyVolumeScalar, size, &involume); 
if(noErr!=err) NSLog(@"error setting volume of channel %d",channels[1]); 

} 

在此先感謝!

回答

3

這將這樣的伎倆 - 我只是複製並粘貼它從我的項目之一,所以有一些多餘的變量和函數,但你應該能夠得到它的要點:

BOOL gotMaster = NO; 
BOOL gotLeft = NO; 
BOOL gotRight = NO; 
float volume = 1, lvolume = 1, rvolume = 1; 
float inVolume = 1, inLvolume = 1, inRvolume = 1; 
OSStatus result = noErr; 
AudioObjectPropertyAddress thePropertyAddress; 
UInt32 thePropSize = sizeof(Float32); 
thePropertyAddress.mSelector = kAudioDevicePropertyVolumeScalar; 
thePropertyAddress.mScope = kAudioDevicePropertyScopeOutput;  
thePropertyAddress.mElement = kAudioObjectPropertyElementMaster; 
// see if the device supports volume control, if so, then get it 
if (AudioObjectHasProperty(_targetDevice, &thePropertyAddress)) { 
    result = AudioObjectGetPropertyData(_targetDevice, &thePropertyAddress, 0, NULL, &thePropSize, &volume); 
    if (result) { cwlog("AudioDeviceManager: Error in AudioObjectGetPropertyData: %d", result); return -1; } 
    gotMaster = YES; 
} else { 
    cwlog("AudioDeviceManager: The target device does not support getting the master volume"); 
} 
// see if the device supports left channel volume control, if so, then get it 
thePropertyAddress.mElement = 1; 
if (AudioObjectHasProperty(_targetDevice, &thePropertyAddress)) { 
    result = AudioObjectGetPropertyData(_targetDevice, &thePropertyAddress, 0, NULL, &thePropSize, &lvolume); 
    if (result) { cwlog("AudioDeviceManager: Error in AudioObjectGetPropertyData: %d", result); return -1; } 
    gotLeft = YES; 
} else { 
    cwlog("AudioDeviceManager: The target device does not support getting the left channel volume"); 
} 
thePropertyAddress.mElement = 2; 
// see if the device supports right channel volume control, if so, then get it 
if (AudioObjectHasProperty(_targetDevice, &thePropertyAddress)) { 
    result = AudioObjectGetPropertyData(_targetDevice, &thePropertyAddress, 0, NULL, &thePropSize, &rvolume); 
    if (result) { cwlog("AudioDeviceManager: Error in AudioObjectGetPropertyData: %d", result); return -1; } 
    gotRight = YES; 
} else { 
    // if the device does not support master volume control, do nothing 
    cwlog("AudioDeviceManager: The target device does not support getting the right channel volume"); 
} 
if(!gotMaster && !gotLeft && !gotRight) { 
    cwlog("AudioDeviceManager: Could not get any volume settings for %s (%u)", [[self nameOfDevice:_targetDevice] UTF8String], _targetDevice); 
    return -1; 
} 
+0

這是破壞StackOverflow的答案類型。你打算給示例代碼和指針,而不是複製粘貼,這些複製粘貼將直接複製。 :-( – Entalpi 2014-05-07 17:34:36

+3

我從三年級開始就編程計算機(我是41歲),說實話,我已經從StackOverflow獲得瞭如此多的幫助,因爲我已經將我的技能轉化爲iOS和Cocoa,如果其他人沒有問題編程需要真正的工作,至少從我的角度來看,我們都可以使用所有我們可以得到的幫助。我不認爲沒有學習蘋果公司改變音頻設備屬性的複雜碳方案爲了防止「noob」進展,我們試着相互對待,呃? – Colin 2014-05-24 02:35:40

相關問題