如何使用swift 2.2

时间:2016-07-30 16:38:19

标签: swift avplayer avplayerviewcontroller

我设置了一个'performSegueWithIdentifier'来从一个按钮调用。视频的URL位于字符串'self.specimen.filmMovieLink'中。如何将此字段传递给AVController?我已经设置了一个名为'newMovie'的segue,并将AV视图控制器场景添加到故事板(没有任何代码或附加文件来控制该场景)。

我需要从performSegueWithIdentifier发送它(“NewMovie”,sender:specimenAnnotation)

如果我添加了URL和带有工作URL的直接字符串,它会打开AVPlayer控制器,但在我尝试此代码时不播放视频:
   performSegueWithIdentifier(“watchMovie”,sender:specimenAnnotation) let url = NSURL(字符串:“https://www.example.com/video.mp4”)

此代码正在使用来自不同视图控制器的字段将视频网址转换为AVController,而不是使用“prepareForSegue”:

  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!)

    }
}

我尝试将它改编为'performSegueWithIdentifier',但我是初学者,无法让它工作。我如何调整该代码(或我需要什么新代码)将我的字段(self.specimen.filmMovieLink)传递给带有'performSegueWithIdentifier'而不是'prepareForSegue'的AV控制器?

1 个答案:

答案 0 :(得分:1)

我接近它的方式,我会创建一个属性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 
    }
}

在您的ViewDidLoad或您的UIButton操作方法的内部,您可以在下面调用此行

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

然后

 @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,如果您想使用tableView,您可以查看 my github project