AVCaptureDevice devicesWithMediaType: Does not update after added/removed camera

1.2k views Asked by At

I'm building an OS X app and since it uses the camera I'd like to know when a new one becomes available or when one disappears (is unplugged). My hope with the following code was that when I plugged in a new USB camera or unplugged one I would get a different count in devices available. However, the count never changes. If I start with no camera attached it outputs 1 (for the built-in camera) and still stays 1 after I have plugged in a new USB camera. Likewise, if I start with 2 and unplug it with the app running it still says 2 after the camera has been unplugged.

If I restart the whole app it always reports the correct number of devices.

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);
}

How can I get an updated count of devices while my app is running?

I also tried to subscribe to

AVCaptureDeviceWasConnectedNotification and 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");
}

But I did not receive any callbacks after plugging in/unplugging a USB camera.

1

There are 1 answers

0
user1137608 On

I have the same problem, and I tried the following code in Mac OSX UI app, and when I plug/unplug bluetooth device, I can receive the AVCaptureDeviceWasConnectedNotification and AVCaptureDeviceWasDisconnectedNotification notifacations.

I guess, in order to receive the above notifications, [AVCaptureDevice devices] (or some other similar methods)should be called(devices method is deprecated).

#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