2

我知道机场可以被 CoreWLAN 框架关闭。

所以,我认为可能有与蓝牙设备和声音设备相关的功能或框架。

我怎样才能关闭这些设备?

4

1 回答 1

0

我假设你“不能有权力所以它不能说话”,你的意思是你只是想让扬声器静音。我在这里找到了一些简洁的示例代码,使用 CoreAudio 将系统的默认扬声器静音:http ://cocoadev.com/index.pl?SoundVolume

我冒昧地将其转换为纯 C 并尝试了它。

#import <CoreAudio/CoreAudio.h>
#import <stdio.h>

// getting system volume

float getVolume() {
    float           b_vol;
    OSStatus        err;
    AudioDeviceID   device;
    UInt32          size;
    UInt32          channels[2];
    float           volume[2];

    // get device
    size = sizeof device;
    err = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice, &size, &device);
    if(err!=noErr) {
        printf("audio-volume error get device\n");
        return 0.0;
    }

    // try set master volume (channel 0)
    size = sizeof b_vol;
    err = AudioDeviceGetProperty(device, 0, 0, kAudioDevicePropertyVolumeScalar, &size, &b_vol);    //kAudioDevicePropertyVolumeScalarToDecibels
    if(noErr==err) return b_vol;

    // otherwise, try seperate channels
    // get channel numbers
    size = sizeof(channels);
    err = AudioDeviceGetProperty(device, 0, 0,kAudioDevicePropertyPreferredChannelsForStereo, &size,&channels);
    if(err!=noErr) printf("error getting channel-numbers\n");

        size = sizeof(float);
        err = AudioDeviceGetProperty(device, channels[0], 0, kAudioDevicePropertyVolumeScalar, &size, &volume[0]);
        if(noErr!=err) printf("error getting volume of channel %d\n",channels[0]);
            err = AudioDeviceGetProperty(device, channels[1], 0, kAudioDevicePropertyVolumeScalar, &size, &volume[1]);
            if(noErr!=err) printf("error getting volume of channel %d\n",channels[1]);

                b_vol = (volume[0]+volume[1])/2.00;

                return  b_vol;
}


// setting system volume
void setVolume(float involume) {
    OSStatus        err;
    AudioDeviceID       device;
    UInt32          size;
    Boolean         canset  = false;
    UInt32          channels[2];
    //float         volume[2];

    // get default device
    size = sizeof device;
    err = AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOutputDevice, &size, &device);
    if(err!=noErr) {
        printf("audio-volume error get device\n");
        return;
    }


    // try set master-channel (0) volume
    size = sizeof canset;
    err = AudioDeviceGetPropertyInfo(device, 0, false, kAudioDevicePropertyVolumeScalar, &size, &canset);
    if(err==noErr && canset==true) {
        size = sizeof involume;
        err = AudioDeviceSetProperty(device, NULL, 0, false, kAudioDevicePropertyVolumeScalar, size, &involume);
        return;
    }

    // else, try seperate channes
    // get channels
    size = sizeof(channels);
    err = AudioDeviceGetProperty(device, 0, false, kAudioDevicePropertyPreferredChannelsForStereo, &size,&channels);
    if(err!=noErr) {
        printf("error getting channel-numbers\n");
        return;
    }

    // set volume
    size = sizeof(float);
    err = AudioDeviceSetProperty(device, 0, channels[0], false, kAudioDevicePropertyVolumeScalar, size, &involume);
    if(noErr!=err) printf("error setting volume of channel %d\n",channels[0]);
        err = AudioDeviceSetProperty(device, 0, channels[1], false, kAudioDevicePropertyVolumeScalar, size, &involume);
        if(noErr!=err) printf("error setting volume of channel %d\n",channels[1]);

}




int main() {
    printf("The system's volume is currently %f\n", getVolume());
    printf("Setting volume to 0.\n");
    setVolume(0.0f);
    return 0;
}

我运行它并得到了这个:

[04:29:03] [william@enterprise ~/Documents/Programming/c]$ gcc -framework CoreAudio -o mute.o coreaudio.c 
.. snipped compiler output..
[04:29:26] [william@enterprise ~/Documents/Programming/c]$ ./mute.o 
The system's volume is currently 0.436749
Setting volume to 0.

希望这会让您朝着正确的方向前进。

于 2011-03-10T10:34:56.877 回答