问题描述
在OS X中,如何确定其他应用程序或进程正在使用相机或麦克风?除非其他应用程序已锁定该设备,否则以下内容似乎不起作用.
In OS X, how can I tell if the camera or microphone is in use by another application or process? The following doesn't seem to work unless the other application has locked the device.
NSArray *devices = [AVCaptureDevice devices];
for (AVCaptureDevice *device in devices) {
NSLog(@"In use by other application %hhd", [device isInUseByAnotherApplication]);
}
推荐答案
您可以使用CoreAudio来检查是否正在使用麦克风.
You can use CoreAudio to check if microphone is in use or not.
AudioObjectPropertyAddress propertyAddress = {kAudioHardwarePropertyDevices,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
Now Iterate through all the devices and fetch property data kAudioDevicePropertyDeviceIsRunningSomewhere
CFBooleanRef deviceIsRunning = NULL;dataSize = sizeof(deviceIsRunning);propertyAddress.mSelector = kAudioDevicePropertyDeviceIsRunningSomewhere;状态= AudioObjectGetPropertyData(audioDevices [i],& propertyAddress,0,NULL,& dataSize,& deviceIsRunning);
检查deviceIsRunning变量.
Check deviceIsRunning variable.
我对视频设备一无所知.但是,如果我找到解决办法,我将更新我的答案.
I don't have idea about video device. But i will update my answer if i find some solution.
希望获得帮助.
这篇关于如何判断相机是否正在被其他进程使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!