skip to Main Content

I have two viewcontrollers: GetStartedViewController (root viewController) and TrimVideoViewController. I pick a video from the root one using PHPicker and I need to play that video in the second viewController. The problem is that when I provide the url to AVFoundation nothing happens. I am at a loss.

I’ve scoured the internet and cannot find an answer to this.

I saw a single solution to this saying I need to copy and save to another directory and use that directory with AVFoundation. It still didn’t work.

I also searched for a framework that could maybe play videos from gallery, but most of them only played using HTTP links.

can anyone help me with this?

Here’s the GetStartedViewController

Here’s the TrimVideoViewController

2

Answers


  1. Chosen as BEST ANSWER

    Okay, after consulting with a more experienced iOS developer, we found that the url that was being passed by PHPicker was not suitable for AVFoundation. Instead we wrote this function that relocates the video and makes it suitable for AVFoundation.

    private func getVideo(from itemProvider: NSItemProvider, typeIdentifier: String) {
        itemProvider.loadFileRepresentation(forTypeIdentifier: typeIdentifier) { url, error in
            if let error = error {
                print(error.localizedDescription)
            }
            
            guard let url = url else { return }
            
            let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first
            guard let targetURL = documentsDirectory?.appendingPathComponent(url.lastPathComponent) else { return }
            
            do {
                if FileManager.default.fileExists(atPath: targetURL.path) {
                    try FileManager.default.removeItem(at: targetURL)
                }
                
                try FileManager.default.copyItem(at: url, to: targetURL)
                
                DispatchQueue.main.async {
                    let vc = UIStoryboard.init(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "TrimVideooViewController") as? TrimVideooViewController
                    vc!.videoURL = targetURL
                    self.navigationController?.pushViewController(vc!, animated: true)
                }
            } catch {
                print(error.localizedDescription)
            }
        }
    }
    

    enjoy!


  2. please take note that you have fetched the URL but you didn’t set self.url = url
    changing your vc!.videoURL = url to the following should make a difference

    vc!.videoURL = url
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search