又一个 Mac Catalyst 移植问题。在 iOS 中完美运行以获取用于视频目的的 AVCaptureDevice 的代码在 macOS 上运行时失败并返回 nil。
演示此问题的最简单方法是使用 Apple 自己的AvCam 演示应用程序。它在 Swift 和 Objective-C 中都提供。两者都以同样的方式失败。选中“Mac”复选框并在 Mac 上构建和运行。授予相机权限,然后注意控制台中的错误。
在 Swift 版本中,查看configureSession
CameraViewController 的方法。这些行:
var defaultVideoDevice: AVCaptureDevice?
// Choose the back dual camera, if available, otherwise default to a wide angle camera.
if let dualCameraDevice = AVCaptureDevice.default(.builtInDualCamera, for: .video, position: .back) {
defaultVideoDevice = dualCameraDevice
} else if let backCameraDevice = AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: .back) {
// If a rear dual camera is not available, default to the rear wide angle camera.
defaultVideoDevice = backCameraDevice
} else if let frontCameraDevice = AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: .front) {
// If the rear wide angle camera isn't available, default to the front wide angle camera.
defaultVideoDevice = frontCameraDevice
}
nil
即使已授予相机权限,也总是导致。
或者看configureSession
Objective-C版本中AVCamCameraViewController类的方法。尝试获取 AVCaptureDevice 的相同代码失败。
我也尝试过使用该AVCaptureDevice default(for:)
方法,并且尝试过使用AVCaptureDevice.DiscoverySession devices
. 在 Mac 上运行时所有尝试均失败,而在 iOS 设备上运行时所有尝试均有效。
更有趣的是,在我自己的与 AvCam 演示应用程序存在相同问题的应用程序中,我可以使用 UIImagePickerController 很好地拍照和捕捉视频。这意味着一般来说,Mac Catalyst 应用程序可以使用 Mac 的相机拍摄照片和视频。
那么问题来了,获取视频的 AVCaptureDevice 需要进行哪些更改?谁能弄清楚 AvCam 演示应用程序在使用 Mac Catalyst 运行时需要进行哪些更改才能使其正常工作?