【问题标题】:AVCaptureDevice devicesWithMediaType: Does not update after added/removed cameraAVCaptureDevice devicesWithMediaType:添加/删除相机后不更新
【发布时间】: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摄像头后我没有收到任何回调。

【问题讨论】:

标签: macos camera video-capture


【解决方案1】:

我有同样的问题,我在 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多