macos - 如何判断相机是否正在被其他进程使用?

标签 macos cocoa avfoundation

在 OS X 中,如何判断摄像头或麦克风是否正在被其他应用程序或进程使用?除非其他应用程序锁定了设备,否则以下操作似乎不起作用。

NSArray *devices = [AVCaptureDevice devices];

for (AVCaptureDevice *device in devices) {
    NSLog(@"In use by other application %hhd", [device isInUseByAnotherApplication]);
}

最佳答案

您可以使用CoreAudio来检查麦克风是否正在使用。

AudioObjectPropertyAddress 属性地址 = { k音频硬件属性设备, kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMaster };

UInt32 dataSize = 0;
OSStatus status = AudioObjectGetPropertyDataSize(kAudioObjectSystemObject, &propertyAddress, 0, NULL, &dataSize);
if(kAudioHardwareNoError != status) {
    fprintf(stderr, "AudioObjectGetPropertyDataSize (kAudioHardwarePropertyDevices) failed: %i\n", status);
    //return NULL;
    return;
}

UInt32 deviceCount = (UInt32)(dataSize / sizeof(AudioDeviceID));

AudioDeviceID *audioDevices = (AudioDeviceID*)(malloc(dataSize));
if(NULL == audioDevices) {
    fputs("Unable to allocate memory", stderr);
    return;
}

status = AudioObjectGetPropertyData(kAudioObjectSystemObject, &propertyAddress, 0, NULL, &dataSize, audioDevices);
if(kAudioHardwareNoError != status) {
    fprintf(stderr, "AudioObjectGetPropertyData (kAudioHardwarePropertyDevices) failed: %i\n", status);
    free(audioDevices), audioDevices = NULL;
    return ;
}

CFMutableArrayRef inputDeviceArray = CFArrayCreateMutable(kCFAllocatorDefault, deviceCount, &kCFTypeArrayCallBacks);
if(NULL == inputDeviceArray) {
    fputs("CFArrayCreateMutable failed", stderr);
    free(audioDevices), audioDevices = NULL;
    return ;
}`

现在迭代所有设备并获取属性数据kAudioDevicePropertyDeviceIsRunningSomewhere

CFBooleanRef deviceIsRunning = NULL; dataSize = sizeof(deviceIsRunning); propertyAddress.mSelector = kAudioDevicePropertyDeviceIsRunningSomewhere; 状态 = AudioObjectGetPropertyData(audioDevices[i], &propertyAddress, 0, NULL, &dataSize, &deviceIsRunning);

检查 deviceIsRunning 变量。

我不知道视频设备。但如果我找到解决方案,我会更新我的答案。

希望这有帮助。

关于macos - 如何判断相机是否正在被其他进程使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37470201/

相关文章:

macos - 将 Cocoa 与 AppleScript 结合使用

objective-c - 使用下划线作为前缀命名实例变量在 Cocoa (Objective-C) 中是否有任何副作用?

swift/Spritekit : Play music through specific scenes

ios - 如何在转换为视频时为图像制作动画

XCODE 8 升级后的 AVFoundation 导入警告

macos - 在 Swift 中将点击处理程序添加到 NSTextField

macos - 高写入 I/O "Pulsing",带有 dtrace 错误

c# - 使用 C# 的 MacOs 事件窗口标题

swift - MacOS 中的 NSSlider 缩放功能不起作用

cocoa - 如何更改 NSProgressIndicator 的高度?