Mar*_*tin 9 ios avcapturesession swift
I'm using AVCaptureSession to capture video.
I want to light on the torch during the whole session, but once the session is started, the light turns automatically off.
这里有很多帖子展示了如何打开手电筒。它有效,除非捕获会话已启动。
这是我开始会话的方式
guard let camera = AVCaptureDevice.default(for: .video) else { return }
self.captureSession.beginConfiguration()
let deviceInput = try AVCaptureDeviceInput(device: camera)
self.captureSession.addInput(deviceInput)
let videoOutput = AVCaptureVideoDataOutput()
videoOutput.setSampleBufferDelegate(self, queue: DispatchQueue(label: "com.axelife.axcapturer.samplebufferdelegate"))
self.captureSession.addOutput(videoOutput)
try camera.setLight(on: true)
self.captureSession.commitConfiguration()
DispatchQueue(label: "capturesession").async {
self.captureSession.startRunning()
}
Run Code Online (Sandbox Code Playgroud)
还有我开灯的代码
extension AVCaptureDevice {
func setLight(on: Bool) throws {
try self.lockForConfiguration()
if on {
try self.setTorchModeOn(level: 1)
}
else {
self.torchMode = .off
}
self.unlockForConfiguration()
}
}
Run Code Online (Sandbox Code Playgroud)
使用该代码,灯会在 < 0.5 秒内打开,然后自动关闭。
Mar*_*tin 13
好的,我想通了。
手电筒必须在会议开始后点燃。
所以而不是:
try camera.setLight(on: true)
self.captureSession.commitConfiguration()
DispatchQueue(label: "capturesession").async {
self.captureSession.startRunning()
}
Run Code Online (Sandbox Code Playgroud)
做就是了
self.captureSession.commitConfiguration()
DispatchQueue(label: "capturesession").async {
self.captureSession.startRunning()
try camera.setLight(on: true)
}
Run Code Online (Sandbox Code Playgroud)