AVCaptureDevice 不支持的帧持续时间 iPhone 11 Pro Max.


我一直在使用AVCaptureDevice

新iPhone 11之前设置的方法activeVideoMinFrameDuration没有任何问题,但是使用新的iPhone 11 Pro max(实际设备(测试相同的代码,该应用程序崩溃并显示错误消息:-

由于未捕获的异常"NSInvalidArgumentException"而终止应用程序,原因:-[AVCaptureDevice setActiveVideoMinFrameDuration:] 不支持的帧持续时间 - 使用 -activeFormat.videoSupportedFrameRateRanges 发现有效范围

相同的代码适用于旧手机。实际上我正在使用Apple文档中的确切示例代码

func configureCameraForHighestFrameRate(device: AVCaptureDevice) {
var bestFormat: AVCaptureDevice.Format?
var bestFrameRateRange: AVFrameRateRange?
for format in device.formats {
for range in format.videoSupportedFrameRateRanges {
if range.maxFrameRate > bestFrameRateRange?.maxFrameRate ?? 0 {
bestFormat = format
bestFrameRateRange = range
}
}
}
if let bestFormat = bestFormat, 
let bestFrameRateRange = bestFrameRateRange {
do {
try device.lockForConfiguration()
// Set the device's active format.
device.activeFormat = bestFormat
// Set the device's min/max frame duration.
let duration = bestFrameRateRange.minFrameDuration
device.activeVideoMinFrameDuration = duration
device.activeVideoMaxFrameDuration = duration
device.unlockForConfiguration()
} catch {
// Handle error.
}
}
}

正如我上面提到的,此代码适用于较旧的设备,但不适用于iPhone 11系列(在11 Pro max上测试(。

有没有人有解决此问题的解决方法或解决方案?

对于将来会遇到相同问题的任何人。以下是我按照调试错误描述解决问题的方法。

// Important part: You must get the supported frame duration and use it to set max and min frame durations. 
let supporrtedFrameRanges = device.activeFormat.videoSupportedFrameRateRanges.first
// Then use it, note that only fall back to bestFrameRateRange if supporrtedFrameRanges is nil.            
device.activeVideoMinFrameDuration = supporrtedFrameRanges?.minFrameDuration ?? bestFrameRateRange.minFrameDuration
device.activeVideoMaxFrameDuration = supporrtedFrameRanges?.maxFrameDuration ?? bestFrameRateRange.maxFrameDuration

最新更新