0

我试图弄清楚如何使用 PHPicker 加载视频。我知道如何使用图像来做到这一点,但我似乎无法弄清楚如何使用视频来做到这一点。此代码仅适用于某些视频,不适用于其他视频。

这是我的代码

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

4

0 回答 0