(设置/获取)系统声音

-(AudioDeviceID)getDefaultOutputDeviceID
{
    AudioDeviceID outputDeviceID = kAudioObjectUnknown;
    
    // get output device device
    OSStatus status = noErr;
    AudioObjectPropertyAddress propertyAOPA;
    propertyAOPA.mScope = kAudioObjectPropertyScopeGlobal;
    propertyAOPA.mElement = kAudioObjectPropertyElementMaster;
    propertyAOPA.mSelector = kAudioHardwarePropertyDefaultOutputDevice;
    
    if (!AudioHardwareServiceHasProperty(kAudioObjectSystemObject, &propertyAOPA))
    {
        printf("Cannot find default output device!");
        return outputDeviceID;
    }
    
    status = AudioHardwareServiceGetPropertyData(kAudioObjectSystemObject, &propertyAOPA, 0, NULL, (UInt32[]){sizeof(AudioDeviceID)}, &outputDeviceID);
    
    if (status != 0) 
    {
        printf("Cannot find default output device!");
    }
    return outputDeviceID;
}

// 获取系统声音:
-(float) getVolume
{
    Float32 outputVolume;
    
    OSStatus status = noErr;
    AudioObjectPropertyAddress propertyAOPA;
    propertyAOPA.mElement = kAudioObjectPropertyElementMaster;
    propertyAOPA.mSelector = kAudioHardwareServiceDeviceProperty_VirtualMasterVolume;
    propertyAOPA.mScope = kAudioDevicePropertyScopeOutput;
    
    AudioDeviceID outputDeviceID = [self getDefaultOutputDeviceID];
    
    if (outputDeviceID == kAudioObjectUnknown)
    {
        printf("Unknown device");
        return 0.0;
    }
    
    if (!AudioHardwareServiceHasProperty(outputDeviceID, &propertyAOPA))
    {
        printf("No volume returned for device 0x%0x", outputDeviceID);
        return 0.0;
    }
    
    status = AudioHardwareServiceGetPropertyData(outputDeviceID, &propertyAOPA, 0, NULL, (UInt32[]){sizeof(Float32)}, &outputVolume);
    
    if (status)
    {
        printf("No volume returned for device 0x%0x", outputDeviceID);
        return 0.0;
    }
    
    if (outputVolume < 0.0 || outputVolume > 1.0) return 0.0;
    
    return outputVolume;
}

// 设置系统声音:
-(void)setSystemVolumeValue:(Float32)newVolume
{
    if (newVolume < 0.0 || newVolume > 1.0)
    { 
        NSLog(@"Requested volume out of range (%.2f)", newVolume);
        return;
    }
    // get output device device
    UInt32 propertySize = 0;
    OSStatus status = noErr;
    AudioObjectPropertyAddress propertyAOPA; 
    propertyAOPA.mElement = kAudioObjectPropertyElementMaster;
    propertyAOPA.mScope = kAudioDevicePropertyScopeOutput;
    if (newVolume < 0.001)
    {
        NSLog(@"Requested mute");
        propertyAOPA.mSelector = kAudioDevicePropertyMute;
    } 
    else 
    { 
        NSLog(@"Requested volume %.2f", newVolume); 
        propertyAOPA.mSelector = kAudioHardwareServiceDeviceProperty_VirtualMasterVolume; 
    }
    AudioDeviceID outputDeviceID = [self getDefaultOutputDeviceID];
    if (outputDeviceID == kAudioObjectUnknown)
    {
        NSLog(@"Unknown device"); return; 
    }
    if (!AudioHardwareServiceHasProperty(outputDeviceID, &propertyAOPA)) 
    {
        NSLog(@"Device 0x%0x does not support volume control", outputDeviceID); 
        return; 
    }
    Boolean canSetVolume = NO;
    status = AudioHardwareServiceIsPropertySettable(outputDeviceID, &propertyAOPA, &canSetVolume);
    if (status || canSetVolume == NO) 
    { 
        NSLog(@"Device 0x%0x does not support volume control", outputDeviceID); 
        return; 
    }
    if (propertyAOPA.mSelector == kAudioDevicePropertyMute)
    {
        propertySize = sizeof(UInt32);
        UInt32 mute = 1;
        status = AudioHardwareServiceSetPropertyData(outputDeviceID, &propertyAOPA, 0, NULL, propertySize, &mute);  
    }
    else
    {
        propertySize = sizeof(Float32);
        status = AudioHardwareServiceSetPropertyData(outputDeviceID, &propertyAOPA, 0, NULL, propertySize, &newVolume);
        if (status)
        {
            NSLog(@"Unable to set volume for device 0x%0x", outputDeviceID); 
        }
        // make sure we're not muted 
        propertyAOPA.mSelector = kAudioDevicePropertyMute; 
        propertySize = sizeof(UInt32);
        UInt32 mute = 0;
        if (!AudioHardwareServiceHasProperty(outputDeviceID, &propertyAOPA)) 
        {
            NSLog(@"Device 0x%0x does not support muting", outputDeviceID); 
            return;
        }
        Boolean canSetMute = NO;
        status = AudioHardwareServiceIsPropertySettable(outputDeviceID, &propertyAOPA, &canSetMute);
        if (status || !canSetMute)
        {
            NSLog(@"Device 0x%0x does not support muting", outputDeviceID); 
            return; 
        }
        status = AudioHardwareServiceSetPropertyData(outputDeviceID, &propertyAOPA, 0, NULL, propertySize, &mute); 
    }
    if (status) 
    {
        NSLog(@"Unable to set volume for device 0x%0x", outputDeviceID); 
    }
}

原文链接:http://cocoadev.com/wiki/SoundVolume

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值