WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

Play video fetched from the photo App Library

Forums > SwiftUI

Hi, I am struggeling playing a video from a PHAsset fetched from the photo library. Web url or url from bundle works fine (https://www.hackingwithswift.com/quick-start/swiftui/how-to-play-movies-with-videoplayer). There are many examples for that. But I cannot find any hints to create an URL for a PHAsset. I hope someone can give me an advice. THX best regards Sven


I found this here

PHCachingImageManager().requestAVAsset(forVideo: phAsset, options: nil) { (avAsset, , ) in print(avAsset) }

but avAsset is always nil :-(


Assuming you're using picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]), this was unexpectedly complicated, but some important things I discovered:

  1. The video may not be available on the device and needs to be downloaded from iCloud, which is an asynchronous operation.
  2. Once you get the URL you need to store it immediately, otherwise it will no longer be available.

Some pointers... given let videoId = UTType.movie.identifier:

  1. Grab the item provider, e.g. let provider = results.first?.itemProvider
  2. Ensure it's a video provider.hasItemConformingToTypeIdentifier(videoId).
  3. Load the file, optionally getting the progress object let progress = provider.loadFileRepresentation(forTypeIdentifier: videoId) {. The progress object is useful to display within your app because depending on the video it can take a few seconds or several minutes to download.
  4. Store the file to disk within the loadFileRepresentation method and pass the new URL to wherever your app needs it.


Save 50% in my Black Friday sale.

SAVE 50% To celebrate WWDC22, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.