macos - AVCaptureDevice devicesWithMediaType:添加删除摄像头后不更新

macos - AVCaptureDevice devicesWithMediaType:添加删除摄像头后不更新,第1张

我正在构建一个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摄像头后,我没有收到任何回调信息。

最佳答案:

1 个答案:

答案 0 :(得分:0)

我有相同的问题,我在Mac OSX UI应用程序中尝试了以下代码,当我插入/拔出蓝牙设备时,我可以收到AVCaptureDeviceWasConnectedNotification和AVCaptureDeviceWasDisconnectedNotification 通知。

我想,为了接收以上通知,应该调用[AVCaptureDevice设备](或其他类似方法)(不建议使用devices方法)。

#import "AppDelegate.h"
#import <AVFoundation/AVFoundation.h>

@interface AppDelegate ()
@property (weak) IBOutlet NSWindow *window;
@end

@implementation AppDelegate {
    NSNotificationCenter *notiCenter;
    id add;
    id remove;
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    NSArray *devices= [AVCaptureDevice devices];
    for (AVCaptureDevice *device in devices) {
        NSLog(@"Devicename:%@",[device localizedName]);
    }

    notiCenter = [NSNotificationCenter defaultCenter];
    add =[notiCenter addObserverForName:AVCaptureDeviceWasConnectedNotification
                                        object:nil
                                         queue:[NSOperationQueue mainQueue]
                                    usingBlock:^(NSNotification *note)
                                                {
                                                    NSLog(@"device---add");
                                                }];
    remove =[notiCenter addObserverForName:AVCaptureDeviceWasDisconnectedNotification
                                         object:nil
                                            queue:[NSOperationQueue mainQueue]
                                     usingBlock:^(NSNotification *note)
                                                {
                                                    NSLog(@"device---remove");
                                                }];

}

- (void)applicationWillTerminate:(NSNotification *)aNotification {
    [notiCenter removeObserver:add];
    [notiCenter removeObserver:remove];
}

@end
本文经用户投稿或网站收集转载,如有侵权请联系本站。

发表评论

0条回复