AVCaptureDevice devicesWithMediaType:添加/删除摄像机后不更新

问题描述 投票:5回答: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相机后,我没有收到任何回调。

macos camera video-capture
1个回答
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
© www.soinside.com 2019 - 2024. All rights reserved.