Dimitri T
Dimitri T

Reputation: 951

how to pass video field to AVPlayer controller from 'performSegueWithIdentifier' button, with swift 2.2

I have a 'performSegueWithIdentifier' set up to be called from a button. A URL for a video is in the string 'self.specimen.filmMovieLink'. How do I pass this field through to the the AVController? I have set up a segue called 'newMovie' and added the AV View Controller scene to the storyboard (without any code or attached file to control that scene).

I need to send it through from performSegueWithIdentifier("NewMovie", sender: specimenAnnotation)

If I add the URL and a direct string with a working URL it opens the AVPlayer Controller but does not play the video, when I tried this code:
performSegueWithIdentifier("watchMovie", sender: specimenAnnotation) let url = NSURL(string: "https://www.example.com/video.mp4")

This code is working to segue the video URL to the AVController with my fields from a different view controller, using 'prepareForSegue' instead:

  override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        let destination = segue.destinationViewController as!
        AVPlayerViewController
        let url = NSURL(string: self.specimen.filmMovieLink)
        destination.player = AVPlayer(URL: url!)

    }
}

I tried adapting it for the 'performSegueWithIdentifier' but am a beginner and couldn't get it to work. How can I adapt that code (or what new code do I need) to pass my field (self.specimen.filmMovieLink) to the AV controller with the 'performSegueWithIdentifier' instead of this 'prepareForSegue'?

Upvotes: 0

Views: 644

Answers (1)

Lamour
Lamour

Reputation: 3040

The way I would approach it, I would create a property Observer:

 // create a property observer that is set to nil then would change until something was assigned it to 
var myURL:URL? = nil {
    willSet(newValue) {
      print("newvalue \(newValue)")
    }
    didSet{
     // could do more stuff if needed it 
    }
}

Either in your ViewDidLoad or Inside of your UIButton action method your could call this line below

        myURL = URL(string: "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")

then

 @IBAction func MyButton(sender:UIButton) { 
      performSegue(withIdentifier: "segueIdentifier", sender: nil)
}

override func prepare(for segue: UIStoryboardSegue, sender: AnyObject?) {
    let destination = segue.destinationViewController as! AVPlayerViewController
    guard let _url = myURL else {return }
    destination.player = AVPlayer(url: _url)
    destination.player?.play()
}

Voila, if you want to use tableView your could check out my github project

Upvotes: 1

Related Questions