如何在Cocoa中关闭蓝牙设备和声音设备?

时间:2011-03-02 05:46:41

标签: objective-c cocoa macos bluetooth iobluetooth

我知道CoreWLAN框架可以关闭Airport。

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

如何关闭这些设备?

1 个答案:

答案 0 :(得分: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.

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