spf*_*ich 5 macos camera video-capture
我正在构建一个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);
}
Run Code Online (Sandbox Code Playgroud)
如何在我的应用运行时获取更新的设备数量?
我也试过订阅
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");
}
Run Code Online (Sandbox Code Playgroud)
但插入/拔出USB摄像头后,我没有收到任何回调.
小智 0
我有同样的问题,我在 Mac OSX UI 应用程序中尝试了以下代码,当我插入/拔出蓝牙设备时,我可以收到 AVCaptureDeviceWasConnectedNotification 和 AVCaptureDeviceWasDisconnectedNotification 通知。
我想,为了接收上述通知,应该调用[AVCaptureDevice devices](或其他一些类似的方法)(不推荐使用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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
621 次 |
| 最近记录: |