Search code examples
swiftuiimagepickercontroller

I can't get video selected from gallery swift


My code is here

@IBAction func openVideo(_ sender: Any) {
        //selectVideo(delegate: self, sourceType: .savedPhotosAlbum)
        let mediaUI = UIImagePickerController()
        mediaUI.sourceType = .savedPhotosAlbum
        mediaUI.mediaTypes = [kUTTypeMovie as String]
        mediaUI.allowsEditing = true
        mediaUI.delegate = self
        self.present(mediaUI, animated: true, completion: nil)
        
    }

At this part I select video.

extension ViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    
    func processVideo(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]){
        
        let url = info[UIImagePickerController.InfoKey.mediaURL] as! URL
        print("hey") 
        self.dismiss(animated: true) {
            let player = AVPlayer(url: url)
            let vcPlayer = AVPlayerViewController()
            vcPlayer.player = player
            self.present(vcPlayer, animated: true, completion: nil)
            
         
        }
        
    }
}

What am I doing wrong?

I can't get any print in processVideo function. I get this error after selecting the video from gallery.

"Error returned from daemon: Error Domain=com.apple.accounts Code=7 "(null)""

Solution

  • Solved problem by changing name of processvideo func to imagePickerController. It seems like name is very important at delegate functions. I didn't notice that.

    Tip for creating this delegate function write didfinish and you will get prefer function named imagePickerController, use it.