将AVCaptureDevice设置为1 fps时的相机问题

时间:2018-03-27 21:46:39

标签: swift macos avfoundation avcapturesession

我对AVFoundation框架有疑问。每当我将相机的FPS设置为1时,过了一会儿,我的FaceTime HD相机LED会以一秒的间隔开始闪烁。此外,不再调用委托(样本缓冲区委托)。

当我将其设置为2 FPS时,问题不会发生,但对于我正在开发的应用程序,我需要将其设置为1 FPS。

在设置为一个FPS时,只需检查其他人是否有相机问题。如有必要,我会向Apple发布错误报告。 我从2013年中期开始使用Macbook Air,运行macOS 10.13.3。

以下是我用来设置FPS的代码:

/// Set the frame rate of the current av capture session, if it is supported by the camera.
///
/// - Parameter fps: The desired frames per second.
func set(fps: Int) {

    guard let captureDevice = captureDevice,
        let format = captureDevice.activeFormat else { return }
    let doubleFPS = Double(fps)

    // Lock the device before changing the frame rate.
    try? captureDevice.lockForConfiguration()

    // Look for a supported frame rate range that is supported by the camera.
    for range in format.videoSupportedFrameRateRanges as! [AVFrameRateRange] {
        if range.minFrameRate <= doubleFPS &&
            range.maxFrameRate >= doubleFPS {
            let time: CMTime = CMTime(value: 1, timescale: CMTimeScale(fps))
            captureDevice.activeVideoMaxFrameDuration = time
            captureDevice.activeVideoMinFrameDuration = time
            break
        }
    }

    // Unlock the device now that we are done.
    captureDevice.unlockForConfiguration()
}

1 个答案:

答案 0 :(得分:0)

如何处理帧?如果您使用AVCaptureVideoDataOutput进行此操作,则可以丢弃任何不希望达到所需帧速率的帧。如果您要录制到某个文件(例如AVCaptureMovieFileOutput),则不能。

p.s你描述的问题听起来像个bug。理想情况下,您会记录它。