【发布时间】:2015-08-27 16:04:40
【问题描述】:
我正在构建一个 OS X 应用程序,因为它使用相机,所以我想知道什么时候有新的可用或什么时候消失(已拔掉电源)。我对以下代码的希望是,当我插入一个新的 USB 摄像头或拔下一个 USB 摄像头时,我会得到不同的可用设备计数。但是,计数永远不会改变。如果我一开始没有连接相机,它会输出 1(对于内置相机),并且在我插入新的 USB 相机后仍然保持 1。同样,如果我从 2 开始并在运行应用程序的情况下拔下它,在拔下相机后它仍然显示 2。
如果我重新启动整个应用程序,它总是会报告正确的设备数量。
appDelegete:
-(void)startLoop
{
while (true)
{
[self getCams];
usleep(1000000);
}
}
-(void)getCams
{
cameraTest *test = [[cameraTest alloc] init];
[test enumerateDevices];
}
cameraTest:
-(void)enumerateDevices
{
NSArray *devices = [AVCaptureDevice devicesWithMediaType: AVMediaTypeVideo];
NSLog(@"Number of devices found: %lu", (unsigned long)devices.count);
}
如何在我的应用运行时获取更新的设备数量?
我也试过订阅
AVCaptureDeviceWasConnectedNotification 和 AVCaptureDeviceWasDisconnectedNotification
testCamera:
-(id)init
{
self = [super init];
if (self)
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(cameraAdded:)
name:AVCaptureDeviceWasConnectedNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(cameraRemoved:)
name:AVCaptureDeviceWasDisconnectedNotification
object:nil];
}
return self;
}
-(void)cameraAdded:(NSNotification *)notification
{
NSLog(@"A camera was added");
}
-(void)cameraRemoved:(NSNotification *)notification
{
NSLog(@"A camera was removed");
}
但是插入/拔出USB摄像头后我没有收到任何回调。
【问题讨论】:
-
另外,最好不要交叉发布。它使版主免于额外的工作。
-
可能重复的帖子不再存在。我也有同样的问题。只有在启动 Quicktime 时才会触发通知,为什么?
-
同样的问题,有人找到解决办法了吗?
标签: macos camera video-capture