2011-03-02 128 views
2

我已經知道機場可以通過CoreWLAN框架關閉。如何在Cocoa中關閉藍牙設備和聲音設備?

所以,我認爲可能有與藍牙設備和聲音設備相關的功能或框架。

如何關閉該設備?

+1

你是什麼意思「關閉」一個聲音設備?對於藍牙,請查看blueutil的源代碼 - http://www.frederikseiffert.de/blueutil/ – 2011-03-02 05:55:20

+0

我的意思是聲音設備無法通電,因此無法說話。並感謝您的鏈接 – 2011-03-02 06:02:56

回答

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] [[email protected] ~/Documents/Programming/c]$ gcc -framework CoreAudio -o mute.o coreaudio.c 
.. snipped compiler output.. 
[04:29:26] [[email protected] ~/Documents/Programming/c]$ ./mute.o 
The system's volume is currently 0.436749 
Setting volume to 0. 

希望這向您發送了正確的方向。