mac 获取音量、设置音量、获取是否静音,设置静音

1、mac下获取当前电脑输出设备的音量,设置当前电脑输出设备的音量 ,以及设置静音,获取是不是静音

#include <iostream>
#include <vector>

#include <CoreAudio/CoreAudio.h>
#include <AudioToolbox/AudioServices.h>

// 获取默认设备的device_id
// 输入设备:input true
// 输出设备:input false
AudioDeviceID GetDefaultDeviceID(bool input){
    AudioDeviceID device_id;
    AudioObjectPropertySelector selector = input ? kAudioHardwarePropertyDefaultInputDevice
                                                 : kAudioHardwarePropertyDefaultOutputDevice;
    UInt32 propsize = sizeof(AudioDeviceID);
    
    AudioObjectPropertyAddress address = {
        selector,
        kAudioObjectPropertyScopeGlobal,
        kAudioObjectPropertyElementMaster};
    
    OSStatus status = AudioObjectGetPropertyData(kAudioObjectSystemObject, &address, 0, nullptr,
                               &propsize, &device_id);
    if(status != kAudioHardwareNoError){
        std::cout << "get default device id error" << std::endl;
        return 0;
    }
    return device_id;
}

// 获取默认设备的音量
float GetDefaultOutputDeviceVolume(){
    auto device_id = GetDefaultDeviceID(false);
    AudioObjectPropertyAddress virtualMasterVolumeAddress = {
        kAudioHardwareServiceDeviceProperty_VirtualMasterVolume,
        kAudioDevicePropertyScopeOutput,
        kAudioObjectPropertyElementMaster
    };
    float volume;
    UInt32 propsize = sizeof(Float32);
    OSStatus status = AudioObjectGetPropertyData(device_id, &virtualMasterVolumeAddress,
                                                 0, nullptr, &propsize,&volume);
    if(status != kAudioHardwareNoError){
        std::cout << "get default device volume error" << std::endl;
        return 0;
    }
    return volume;
}

// 设备默认输出设备的音量
void SetDefaultOutputDeviceVolume(float volume){
    auto device_id = GetDefaultDeviceID(false);
    AudioObjectPropertyAddress virtualMasterVolumeAddress = {
        kAudioHardwareServiceDeviceProperty_VirtualMasterVolume,
        kAudioDevicePropertyScopeOutput,
        kAudioObjectPropertyElementMaster
    };
    UInt32 propsize = sizeof(Float32);
    OSStatus status = AudioObjectSetPropertyData(device_id, &virtualMasterVolumeAddress,
                                                 0, nullptr, propsize,&volume);
    if(status != kAudioHardwareNoError){
        std::cout << "set default device volume error" << std::endl;
    }
}

// 获取默认设备是不是设置了静音
bool GetDefaultOutputDeviceMute(){
    auto device_id = GetDefaultDeviceID(false);
    UInt32 mute;
    UInt32 propsize = sizeof(UInt32);
    AudioObjectPropertyAddress address = {
        kAudioDevicePropertyMute,
        kAudioDevicePropertyScopeOutput,
        kAudioObjectPropertyElementMaster
    };
    OSStatus status = AudioObjectGetPropertyData(device_id, &address,
                                                 0, nullptr, &propsize,&mute);
    if(status != kAudioHardwareNoError){
        std::cout << "get default device mute error" << std::endl;
        return false;
    }
    // 1 静音
    return mute == 1;
}

// 设置当前默认输出设备是不是为静音
// true : 静音
// false:不是静音
void SetDefaultOutputDeviceMute(bool mute){
    auto device_id = GetDefaultDeviceID(false);
    UInt32 mute_value = mute ? 1 : 0;
    UInt32 propsize = sizeof(UInt32);
    AudioObjectPropertyAddress address = {
        kAudioDevicePropertyMute,
        kAudioDevicePropertyScopeOutput,
        kAudioObjectPropertyElementMaster
    };
    OSStatus status = AudioObjectSetPropertyData(device_id, &address,
                                                 0, nullptr, propsize,&mute_value);
    if(status != kAudioHardwareNoError){
        std::cout << "set default device mute error" << std::endl;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值