(設定/獲取)系統聲音
阿新 • • 發佈:2018-12-10
-(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 = [self 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; }
// 設定系統聲音: -(void)setSystemVolumeValue:(Float32)newVolume { if (newVolume < 0.0 || newVolume > 1.0) { NSLog(@"Requested volume out of range (%.2f)", newVolume); return; } // get output device device UInt32 propertySize = 0; OSStatus status = noErr; AudioObjectPropertyAddress propertyAOPA; propertyAOPA.mElement = kAudioObjectPropertyElementMaster; propertyAOPA.mScope = kAudioDevicePropertyScopeOutput; if (newVolume < 0.001) { NSLog(@"Requested mute"); propertyAOPA.mSelector = kAudioDevicePropertyMute; } else { NSLog(@"Requested volume %.2f", newVolume); propertyAOPA.mSelector = kAudioHardwareServiceDeviceProperty_VirtualMasterVolume; } AudioDeviceID outputDeviceID = [self getDefaultOutputDeviceID]; if (outputDeviceID == kAudioObjectUnknown) { NSLog(@"Unknown device"); return; } if (!AudioHardwareServiceHasProperty(outputDeviceID, &propertyAOPA)) { NSLog(@"Device 0x%0x does not support volume control", outputDeviceID); return; } Boolean canSetVolume = NO; status = AudioHardwareServiceIsPropertySettable(outputDeviceID, &propertyAOPA, &canSetVolume); if (status || canSetVolume == NO) { NSLog(@"Device 0x%0x does not support volume control", outputDeviceID); return; } if (propertyAOPA.mSelector == kAudioDevicePropertyMute) { propertySize = sizeof(UInt32); UInt32 mute = 1; status = AudioHardwareServiceSetPropertyData(outputDeviceID, &propertyAOPA, 0, NULL, propertySize, &mute); } else { propertySize = sizeof(Float32); status = AudioHardwareServiceSetPropertyData(outputDeviceID, &propertyAOPA, 0, NULL, propertySize, &newVolume); if (status) { NSLog(@"Unable to set volume for device 0x%0x", outputDeviceID); } // make sure we're not muted propertyAOPA.mSelector = kAudioDevicePropertyMute; propertySize = sizeof(UInt32); UInt32 mute = 0; if (!AudioHardwareServiceHasProperty(outputDeviceID, &propertyAOPA)) { NSLog(@"Device 0x%0x does not support muting", outputDeviceID); return; } Boolean canSetMute = NO; status = AudioHardwareServiceIsPropertySettable(outputDeviceID, &propertyAOPA, &canSetMute); if (status || !canSetMute) { NSLog(@"Device 0x%0x does not support muting", outputDeviceID); return; } status = AudioHardwareServiceSetPropertyData(outputDeviceID, &propertyAOPA, 0, NULL, propertySize, &mute); } if (status) { NSLog(@"Unable to set volume for device 0x%0x", outputDeviceID); } }