在 OS X 中监听系统音量改变

2019-06-12  本文已影响0人  zhongxiaoyue

iOS中的音频监听

在iOS监听系统音量改变非常简单,只需要监听一个系统的通知就可以了.

NotificationCenter.default.addObserver(self, selector: #selector(ViewController.volumeChange(_:)) , name:Notification.Name(rawValue: "AVSystemController_SystemVolumeDidChangeNotification") , object: nil)

@objc func volumeChange(_ notification:NSNotification) {
    let userInfo = notification.userInfo!
    let volume = userInfo["AVSystemController_AudioVolumeNotificationParameter"] as! Double
}

MacOSX中的音频监听

在macosx中没有对应的系统音量改变通知,只能通过比较底层的方法去监听.
主要分为三步


一. 获取选中音频设备的ID

/// 返回选中的音频输出设备ID
AudioDeviceID defaultOutputDevice()
{
    AudioDeviceID theAnswer = kAudioObjectUnknown;
    UInt32 theSize = sizeof(AudioDeviceID);
    AudioObjectPropertyAddress theAddress;
    
    theAddress.mSelector = kAudioHardwarePropertyDefaultOutputDevice;
    theAddress.mScope = kAudioObjectPropertyScopeGlobal;
    theAddress.mElement = kAudioObjectPropertyElementMaster;
    
    //first be sure that a default device exists
    if (! AudioObjectHasProperty(kAudioObjectSystemObject, &theAddress) )   {
        NSLog(@"Unable to get default Output audio device");
        return theAnswer;
    }
    //get the property 'default output device'
    OSStatus theError = AudioObjectGetPropertyData(kAudioObjectSystemObject, &theAddress, 0, NULL, &theSize, &theAnswer);
    if (theError != noErr) {
        NSLog(@"Unable to get output audio device");
        return theAnswer;
    }
    return theAnswer;
}

二. 添加监听

/// 添加输出音量监听
+ (void)addAudioOutputVolumeListener {

    // 获取当前音频默认输出设备ID
    AudioDeviceID outputDeviceID = defaultOutputDevice();
    
    if (outputDeviceID == kAudioObjectUnknown) {
        return;
    }
    
    
    AudioObjectPropertyAddress outputVolumePropertyAddress = {
        .mScope = kAudioDevicePropertyScopeOutput,
        .mElement = kAudioObjectPropertyElementMaster,
        .mSelector = kAudioHardwareServiceDeviceProperty_VirtualMasterVolume
    };

    // 添加监听
    AudioObjectAddPropertyListener(outputDeviceID, &outputVolumePropertyAddress, onSystemOutputVolumeChange, (__bridge void*)self);
}

/// 系统输出音量改变回调
OSStatus onSystemOutputVolumeChange(AudioObjectID inObjectID,
                                   UInt32 inNumberAddresses,
                                   const AudioObjectPropertyAddress* inAddresses,
                                   void* inClientData) {
    NSLog(@"Volume Change");
    return noErr;
}

三. 移除监听

/// 移除输出音量监听
+ (void)removeAudioOutputVolumeListener {
    
    // 获取当前音频默认输出设备ID
    AudioDeviceID outputDeviceID = defaultOutputDevice();

    if (outputDeviceID == kAudioObjectUnknown) {
        return;
    }
    
    AudioObjectPropertyAddress outputVolumePropertyAddress = {
        .mScope = kAudioDevicePropertyScopeOutput,
        .mElement = kAudioObjectPropertyElementMaster,
        .mSelector = kAudioHardwareServiceDeviceProperty_VirtualMasterVolume
    };
    
    AudioObjectRemovePropertyListener(outputDeviceID, &outputVolumePropertyAddress, onSystemOutputVolumeChange, (__bridge void * _Nullable)(self));
}
上一篇 下一篇

猜你喜欢

热点阅读