如何从osx中的swift应用程序设置系统主卷 [英] How to set systems master volume from a swift app in osx
问题描述
我读了很多关于AudioToolbox和阅读obj-c中的一些源示例。
但是我不能得到它在Swift工作。
有人能告诉我如何在我的swift应用程序中设置主音量的解决方案吗?
此示例:使用XCode以编程方式设置Mac OSX卷
我缺少的 https://developer.apple.com/library/mac/documentation/AudioToolbox/Reference/AudioHardwareServicesReference/index.html#//apple_ref/c/func/AudioHardwareServiceGetPropertyData
感谢您的时间。
这是我从翻译答案以编程方式更改OS X系统卷和使用XCode编程设置Mac OSX卷到Swift(为简洁起见,省略错误检查):
所需框架:
import AudioToolbox
获取默认的输出设备:
VAR defaultOutputDeviceID = AudioDeviceID(0)
VAR defaultOutputDeviceIDSize = UInt32的(sizeofValue(defaultOutputDeviceID ))
VAR getDefaultOutputDevicePropertyAddress = AudioObjectPropertyAddress(
mSelector:AudioObjectPropertySelector(kAudioHardwarePropertyDefaultOutputDevice),
mScope:AudioObjectPropertyScope(kAudioObjectPropertyScopeGlobal),
mElement:AudioObjectPropertyElement(kAudioObjectPropertyElementMaster))
让状态1 = AudioObjectGetPropertyData(
AudioObjectID(kAudioObjectSystemObject),
和getDefaultOutputDevicePropertyAddress,
0,
为零,
和defaultOutputDeviceIDSize,
& defaultOutputDeviceID)
设置音量:
var volume = Float32(0.50)// 0.0 ... 1.0
var volumeSize = UInt32(sizeofValue(volume))
var volumePropertyAddress = AudioObjectPropertyAddress(
mSelector:AudioObjectPropertySelector(kAudioHardwareServiceDeviceProperty_VirtualMasterVolume),
mScope:AudioObjectPropertyScope(kAudioDevicePropertyScopeOutput),
mElement:AudioObjectPropertyElement(kAudioObjectPropertyElementMaster))
让STATUS2 = AudioHardwareServiceSetPropertyData(
defaultOutputDeviceID,
& volumePropertyAddress,
0,
nil,
volumeSize,
& volume)
比对应的C代码更冗长一些,因为许多参数
需要在Swift中明确转换为参数类型。 p>
i read a lot about AudioToolbox and read some source example in obj-c. But i can't get it working in Swift.
Can someone point me to a solution how to set the master volume from within my swift app?
e.g. this example: Setting Mac OSX Volume Programatically using XCode I am missing some example code in https://developer.apple.com/library/mac/documentation/AudioToolbox/Reference/AudioHardwareServicesReference/index.html#//apple_ref/c/func/AudioHardwareServiceGetPropertyData
Thanks for your time.
This is what I got from translating the answers to Change OS X system volume programmatically and Setting Mac OSX Volume Programatically using XCode to Swift (error checking omitted for brevity):
Required framework:
import AudioToolbox
Get default output device:
var defaultOutputDeviceID = AudioDeviceID(0)
var defaultOutputDeviceIDSize = UInt32(sizeofValue(defaultOutputDeviceID))
var getDefaultOutputDevicePropertyAddress = AudioObjectPropertyAddress(
mSelector: AudioObjectPropertySelector(kAudioHardwarePropertyDefaultOutputDevice),
mScope: AudioObjectPropertyScope(kAudioObjectPropertyScopeGlobal),
mElement: AudioObjectPropertyElement(kAudioObjectPropertyElementMaster))
let status1 = AudioObjectGetPropertyData(
AudioObjectID(kAudioObjectSystemObject),
&getDefaultOutputDevicePropertyAddress,
0,
nil,
&defaultOutputDeviceIDSize,
&defaultOutputDeviceID)
Set volume:
var volume = Float32(0.50) // 0.0 ... 1.0
var volumeSize = UInt32(sizeofValue(volume))
var volumePropertyAddress = AudioObjectPropertyAddress(
mSelector: AudioObjectPropertySelector(kAudioHardwareServiceDeviceProperty_VirtualMasterVolume),
mScope: AudioObjectPropertyScope(kAudioDevicePropertyScopeOutput),
mElement: AudioObjectPropertyElement(kAudioObjectPropertyElementMaster))
let status2 = AudioHardwareServiceSetPropertyData(
defaultOutputDeviceID,
&volumePropertyAddress,
0,
nil,
volumeSize,
&volume)
It is a bit more verbose than the corresponding C code because many arguments need to be converted to the parameter type explicitly in Swift.
这篇关于如何从osx中的swift应用程序设置系统主卷的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!