启动 AVCaptureSession 时设备手电筒关闭
Device torch turns off when starting AVCaptureSession
我正在使用 AVCaptureSession 来捕捉视频。
我想在整个session期间点亮手电筒,但是session一启动,手电筒就自动熄灭了。
这里有很多帖子展示了如何打开手电筒。它有效,除非开始捕获 session。
这是我开始 session
的方式
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()
}
还有我的开灯代码
extension AVCaptureDevice {
func setLight(on: Bool) throws {
try self.lockForConfiguration()
if on {
try self.setTorchModeOn(level: 1)
}
else {
self.torchMode = .off
}
self.unlockForConfiguration()
}
}
使用该代码,灯会在 < 0.5 秒内亮起,然后自动关闭。
好的,我明白了。
session 开始后必须点亮手电筒。
所以代替:
try camera.setLight(on: true)
self.captureSession.commitConfiguration()
DispatchQueue(label: "capturesession").async {
self.captureSession.startRunning()
}
就这样
self.captureSession.commitConfiguration()
DispatchQueue(label: "capturesession").async {
self.captureSession.startRunning()
try camera.setLight(on: true)
}
对我来说,当我更改捕获预览层的 videoOrientation
时手电筒会关闭。发生这种情况后,我再次打开它。 (DispatchQueue.main.async
很重要,出于某种原因,如果我将其遗漏,它就不起作用)
previewConnection.videoOrientation = orientation
do {
try captureDevice.lockForConfiguration()
} catch {
print(error)
}
DispatchQueue.main.async { [weak self] in
self?.captureDevice.torchMode = .on
self?.captureDevice.unlockForConfiguration()
}
我正在使用 AVCaptureSession 来捕捉视频。
我想在整个session期间点亮手电筒,但是session一启动,手电筒就自动熄灭了。
这里有很多帖子展示了如何打开手电筒。它有效,除非开始捕获 session。
这是我开始 session
的方式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()
}
还有我的开灯代码
extension AVCaptureDevice {
func setLight(on: Bool) throws {
try self.lockForConfiguration()
if on {
try self.setTorchModeOn(level: 1)
}
else {
self.torchMode = .off
}
self.unlockForConfiguration()
}
}
使用该代码,灯会在 < 0.5 秒内亮起,然后自动关闭。
好的,我明白了。
session 开始后必须点亮手电筒。
所以代替:
try camera.setLight(on: true)
self.captureSession.commitConfiguration()
DispatchQueue(label: "capturesession").async {
self.captureSession.startRunning()
}
就这样
self.captureSession.commitConfiguration()
DispatchQueue(label: "capturesession").async {
self.captureSession.startRunning()
try camera.setLight(on: true)
}
对我来说,当我更改捕获预览层的 videoOrientation
时手电筒会关闭。发生这种情况后,我再次打开它。 (DispatchQueue.main.async
很重要,出于某种原因,如果我将其遗漏,它就不起作用)
previewConnection.videoOrientation = orientation
do {
try captureDevice.lockForConfiguration()
} catch {
print(error)
}
DispatchQueue.main.async { [weak self] in
self?.captureDevice.torchMode = .on
self?.captureDevice.unlockForConfiguration()
}