AVCaptureDevice isTorchActive不能用于React Native



我试着写自己的模块来检测是火炬打开/关闭。

根据文档,我已经尝试运行KVC如下

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if (context == torchActiveObservationContext) {
AVCaptureDevice *thisDevice = (AVCaptureDevice*)object;
[self sendEventWithName:@"TorchEvent" body:@{@"isTorchActive": thisDevice.isTorchActive ? @"ACTIVE" : @"INACTIVE"}];
NSLog( @"Current torch level: %f", thisDevice.torchLevel);
}
else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
NSLog( @"ABCDEF");
}
}
-(id) init {
if (self = [super init]) {
NSLog( @"Start Initialization");
AVCaptureDevice *videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
[videoDevice addObserver:self forKeyPath:@"isTorchActive" options:NSKeyValueObservingOptionNew context:torchActiveObservationContext];
NSLog( @"End Initialization");
// whatever other initialization code ...
}
return self;
}

但我注意到,任何变化被记录,如果我试图打开火炬在控制面板。所以我试着做得更容易,手动检查当前的火炬状态。我创建了一个简单的方法:

RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(isTorchActive)
{
AVCaptureDevice *videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
AVCapturePhotoSettings *photosettings = [AVCapturePhotoSettings photoSettings];

NSLog( videoDevice.isTorchActive ? @"on" : @"off");
NSLog( @"Level : %f", videoDevice.torchLevel);
return videoDevice.isTorchActive ? @"on" : @"off";
}

但是它返回"off"所有的时间,即使火炬最初闪烁。火炬等级为0.0.

我做错了什么?

videoDevice.isTorchActive替换为videoDevice.torchMode == AVCaptureTorchModeOn如果火炬先前从控制中心激活,您可以在第一次运行此代码时获得AVCaptureTorchModeOff

相关内容

  • 没有找到相关文章

最新更新