簡體   English   中英

AVCaptureDevice 不支持的幀持續時間 iPhone 11 Pro Max

[英]AVCaptureDevice Unsupported frame duration iPhone 11 Pro Max

我一直在使用新iPhone 11之前的AVCaptureDevice方法 set 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM