UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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

3      

I found this here

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

but avAsset is always nil :-(

3      

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.

4      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.