PHPicker Load Video

1.2k views Asked by At

I'm trying to figure out how to load videos with PHPicker. I know how to do it with Images but I can't seem to figure out how to do it with videos. This code works only with some videos and not with others.

Here's my code

struct MediaPicker: UIViewControllerRepresentable {
@Binding var video: URL?
    
typealias UIViewControllerType = PHPickerViewController

class Coordinator: NSObject, PHPickerViewControllerDelegate {
    
    var parent: MediaPicker
    
    init(_ parent: MediaPicker) {
        self.parent = parent
    }
    
    func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
                    
        if results.isEmpty {
            picker.dismiss(animated: true)
            return
        }
        let provider = results.first!.itemProvider
        
        if provider.hasItemConformingToTypeIdentifier(UTType.movie.identifier) {
            provider.loadItem(forTypeIdentifier: UTType.movie.identifier) { url, err in
                if err != nil {return}
                if let url = url {
                    DispatchQueue.main.sync {
                        self.parent.video = url as! URL?
                        picker.dismiss(animated: true)
                    }
                }
            }
        }
    }
}

func makeUIViewController(context: Context) -> PHPickerViewController {
    var configuration = PHPickerConfiguration()
    configuration.preferredAssetRepresentationMode = .automatic
    let picker = PHPickerViewController(configuration: configuration)
    picker.delegate = context.coordinator
    return picker
}

func updateUIViewController(_ uiViewController: PHPickerViewController, context: Context) {
    
}

func makeCoordinator() -> Coordinator {
    return MediaPicker.Coordinator(self)
}
}

AVPlayer

1

There are 1 answers

0
dsplatonov On

First of all please set preferredAssetRepresentationMode = .current - it will remove decoding after choosing video (for me it takes several seconds to decode for each video - unacceptable).

Second, you need to copy file to temp folder, because after you leave closure, chosen file will be deleted and you will not be able to work with it. From documentation:

This method writes a copy of the file’s data to a temporary file, which the system deletes when the completion handler returns.

Look at my code, that I wrote for the case with video:

func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
    picker.dismiss(animated: true)
    itemProviders = results.map(\.itemProvider)
    
    itemProviders.forEach {
        guard let typeIdentifier = $0.registeredTypeIdentifiers.first,
              let utType = UTType(typeIdentifier) else { return }
         if utType.conforms(to: .movie) {
            var videoName = ""
            if let suggestedName = $0.suggestedName {
                if utType.conforms(to: .mpeg4Movie) {
                    videoName = suggestedName + ".mp4"
                } else {
                    videoName = suggestedName + ".mov"
                }
            }
            let group = DispatchGroup()
            group.enter()
            $0.loadFileRepresentation(forTypeIdentifier: typeIdentifier) { [weak self] url, error in
                guard let self, let url else { return }
                if let error {
                    print(error.localizedDescription)
                }
                // copying file
                let fm = FileManager.default
                let destination = fm.temporaryDirectory.appendingPathComponent(videoName)
                do {
                    try fm.copyItem(at: url, to: destination)
                } catch {
                    print(error.localizedDescription)
                }
    
                group.leave()
                group.notify(queue: DispatchQueue.global()) {
                    // do staff with video
                    self.output.videoIsPicked(url: destination)
                }
            }
        }
    }