有没有人知道是否可以创建一个侦听器来检测Macs系统卷何时发生变化?如果是这样,你知道我在哪里可以找到一个如何实现这个目标的例子吗?
答案 0 :(得分:2)
所以,希望有更好的方法来解决这个问题,但这是一个概念:
关闭新线程以观察AudioToolbox
中找到的音量属性。然后让它调用某种回调。
#import "AppDelegate.h"
#define kVolumeKey @"currentvolumeforvolumemonitorkey"
@implementation AppDelegate
@synthesize window = _window;
/* Credit to CocoaDev Starts Now */
/* http://www.cocoadev.com/index.pl?SoundVolume */
+(AudioDeviceID)defaultOutputDeviceID
{
AudioDeviceID outputDeviceID = kAudioObjectUnknown;
// get output device device
UInt32 propertySize = 0;
OSStatus status = noErr;
AudioObjectPropertyAddress propertyAOPA;
propertyAOPA.mScope = kAudioObjectPropertyScopeGlobal;
propertyAOPA.mElement = kAudioObjectPropertyElementMaster;
propertyAOPA.mSelector = kAudioHardwarePropertyDefaultOutputDevice;
if (!AudioHardwareServiceHasProperty(kAudioObjectSystemObject, &propertyAOPA))
{
NSLog(@"Cannot find default output device!");
return outputDeviceID;
}
propertySize = sizeof(AudioDeviceID);
status = AudioHardwareServiceGetPropertyData(kAudioObjectSystemObject, &propertyAOPA, 0, NULL, &propertySize, &outputDeviceID);
if(status)
{
NSLog(@"Cannot find default output device!");
}
return outputDeviceID;
}
+(float)volume
{
Float32 outputVolume;
UInt32 propertySize = 0;
OSStatus status = noErr;
AudioObjectPropertyAddress propertyAOPA;
propertyAOPA.mElement = kAudioObjectPropertyElementMaster;
propertyAOPA.mSelector = kAudioHardwareServiceDeviceProperty_VirtualMasterVolume;
propertyAOPA.mScope = kAudioDevicePropertyScopeOutput;
AudioDeviceID outputDeviceID = [AppDelegate defaultOutputDeviceID];
if (outputDeviceID == kAudioObjectUnknown)
{
NSLog(@"Unknown device");
return 0.0;
}
if (!AudioHardwareServiceHasProperty(outputDeviceID, &propertyAOPA))
{
NSLog(@"No volume returned for device 0x%0x", outputDeviceID);
return 0.0;
}
propertySize = sizeof(Float32);
status = AudioHardwareServiceGetPropertyData(outputDeviceID, &propertyAOPA, 0, NULL, &propertySize, &outputVolume);
if (status)
{
NSLog(@"No volume returned for device 0x%0x", outputDeviceID);
return 0.0;
}
if (outputVolume < 0.0 || outputVolume > 1.0) return 0.0;
return outputVolume;
}
/* Thanks CocoaDev! */
- (void)dealloc
{
[super dealloc];
}
- (void)volumeDidChangeToLevel: (CGFloat)level
{
NSLog(@"LEVEL: %f", level);
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0),
^{
[[NSUserDefaults standardUserDefaults] setFloat: [[self class] volume] forKey: kVolumeKey];
[[NSUserDefaults standardUserDefaults] synchronize];
for(CGFloat volLevel = [[self class] volume];; volLevel = [[self class] volume])
{
if (volLevel != [[NSUserDefaults standardUserDefaults] floatForKey: kVolumeKey])
{
[[NSUserDefaults standardUserDefaults] setFloat: [[self class] volume] forKey: kVolumeKey];
[[NSUserDefaults standardUserDefaults] synchronize];
dispatch_async(dispatch_get_main_queue(),
^{
[self volumeDidChangeToLevel: volLevel];
});
}
}
});
}
@end
如果这似乎是您想要的方法,您可能需要考虑使用委托回调或块或其他东西来设置对象。
此解决方案需要一个后台线程来监视持续的更改。它应该是相当低调的,因为这是一个如此简单的检查,但值得一提。