My app needs to load a video that the user selects via PHPickerViewController. It works fine with 99% of videos, but on a few it doesn’t work. It’s strange because other apps appear to load it just fine. Here’s the code to set up the picker:
var config = PHPickerConfiguration(photoLibrary: .shared())
config.selectionLimit = 1
config.filter = PHPickerFilter.any(of: [.videos])
config.preferredAssetRepresentationMode = .current
let phPickerVC = PHPickerViewController(configuration: config)
phPickerVC.delegate = self
// present
Then I load the file representation here:
func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
guard let result = results.first else {
return
}
result.itemProvider.loadFileRepresentation(forTypeIdentifier: UTType.movie.identifier) { url, error in
guard let url, error == nil else {
print(error)
return
}
// process video
}
}
For the item that fails, the error is Error Domain=NSItemProviderErrorDomain Code=-1000 "Cannot load representation of type public.movie" UserInfo={NSLocalizedDescription=Cannot load representation of type public.movie}
If I print result.itemProvider.registeredTypeIdentifiers
, it actually reports "public.jpeg"
so it makes sense that it cannot load it, but it does not make sense that it would be jpeg because it is certainly a video, appears in the photo picker with the video filter, if you long press on it in the photo picker it starts playing the video, etc. The video is actually an iPhone screen recording, but I’ve been able to screen record and use those videos as well.
What’s even stranger is other apps that I can confirm are using the default PHPickerViewController can load the video. I’ve also tried using UIImagePickerViewController
, which does return a URL for the item, even with a .mov
extension, but then attempting to do anything with it fails. For example, Error Domain=AVFoundationErrorDomain Code=-11838 "Operation Stopped" UserInfo={NSLocalizedFailureReason=The operation is not supported for this media., NSLocalizedDescription=Operation Stopped, NSUnderlyingError=0x600000c06ca0 {Error Domain=NSOSStatusErrorDomain Code=-16976 "(null)"}}
.
How can I load this video? This happens on the simulator and on device.