美文网首页
在 OS X 中监听系统音量改变

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

作者: zhongxiaoyue | 来源:发表于2019-06-12 18:04 被阅读0次

    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中没有对应的系统音量改变通知,只能通过比较底层的方法去监听.
    主要分为三步

    • 1.获取选中的音频设备ID.
    • 2.添加该音频设备ID的音量监听方法.
    • 3.移除监听.

    一. 获取选中音频设备的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));
    }
    

    相关文章

      网友评论

          本文标题:在 OS X 中监听系统音量改变

          本文链接:https://www.haomeiwen.com/subject/macrfctx.html