在iPhone X上同时打开手电筒/手电筒和相机



有没有办法在iPhone X上以视频模式运行时保持手电筒打开AVCaptureSession

我有 Swift 4 代码,可以打开手电筒,然后开始从相机获取视频帧。我知道它适用于iPhone 4,5和6。但是对于iPhone X,当我开始捕获会话时,手电筒不会亮起。

session = AVCaptureSession()
if self.session.canSetSessionPreset(AVCaptureSession.Preset.inputPriority) {
self.session.sessionPreset = .inputPriority
}
//This is the wide angle camera device
camera = AVCaptureDevice.default(for: AVMediaType.video)
//I could also use telephoto, same problem
//camera = AVCaptureDevice.default(.builtInTelephotoCamera, for: AVMediaType.video, position: .unspecified)
if camera == nil {
return
}
if self.camera.isTorchModeSupported(.on) {
camera.activeFormat = selectBestFormat(camera.formats)
camera.torchMode = .on
try? camera.setTorchModeOn(level: 1.0)
camera.unlockForConfiguration()
}
let cameraInput = try! AVCaptureDeviceInput(device: self.camera)

let videoOutput = AVCaptureVideoDataOutput()
let captureQueue = DispatchQueue(label: "captureQueue")
videoOutput.setSampleBufferDelegate(self, queue: captureQueue)
videoOutput.videoSettings = [
(kCVPixelBufferPixelFormatTypeKey as AnyObject) as! AnyHashable as! String : Int(kCVPixelFormatType_32BGRA)
]
self.session.addInput(cameraInput)
self.session.addOutput(videoOutput)
//If I don't start the session the torch is on 
self.session.startRunning()

这个问题也有同样的问题。就我而言,问题是手电筒在AVCaptureSession启动后立即关闭。

我尝试在开始会话后打开手电筒,我已经尝试了许多不同的相机配置。我也尝试使用两种不同的相机镜头。对于所有这些配置,指示灯熄灭。

如果没有解决方案,请告诉我是否有为此记录的错误。或者我会记录一个。

也有同样的问题。似乎是iOS 11/iPhone X中的一个错误

我找到了解决方法...不是很优雅,但有效;-(

if let device = captureDevice {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
self.setTorchLevel(device: device, to: 0)
}
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
self.setTorchLevel(device: device, to: 1)
}
}

即使在iPhone上,"您是否尝试过将其关闭并再次打开"也可以正常工作

最新更新