启动AVCaptureSession时设备割炬关闭

时间:2018-12-06 18:12:16

标签: ios swift avcapturesession

我正在使用AVCaptureSession捕获视频。

我想在整个会话过程中点亮手电筒,但是会话开始后,指示灯会自动关闭。

这里有很多帖子显示如何打开手电筒。 除非捕获会话开始,否则它会起作用

这是我开始会议的方式

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秒内打开,然后自动关闭。

1 个答案:

答案 0 :(得分:2)

好,我知道了。

在会议开始后,只须点燃火炬即可。

所以代替:

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)
}
相关问题