【问题标题】:how to pass video field to AVPlayer controller from 'performSegueWithIdentifier' button, with swift 2.2如何使用 swift 2.2 从“performSegueWithIdentifier”按钮将视频字段传递给 AVPlayer 控制器
【发布时间】:2016-12-05 04:41:45
【问题描述】:

我设置了一个“performSegueWithIdentifier”,可以从按钮调用。视频的 URL 在字符串“self.specimen.filmMovieLink”中。如何将此字段传递给 AVController?我设置了一个名为“newMovie”的 segue,并将 AV 视图控制器场景添加到情节提要中(没有任何代码或附加文件来控制该场景)。

我需要从 performSegueWithIdentifier("NewMovie", sender: samplesAnnotation) 发送过来

如果我添加 URL 和带有工作 URL 的直接字符串,它会打开 AVPlayer 控制器但不播放视频,当我尝试此代码时:
performSegueWithIdentifier("watchMovie", 发件人: 标本注解) 让 url = NSURL(string: "https://www.example.com/video.mp4")

此代码正在使用来自不同视图控制器的我的字段将视频 URL 与 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”,但我是初学者,无法使其正常工作。如何调整该代码(或我需要什么新代码)以使用“performSegueWithIdentifier”而不是“prepareForSegue”将我的字段(self.specimen.filmMovieLink)传递给 AV 控制器?

【问题讨论】:

    标签: swift avplayer avplayerviewcontroller


    【解决方案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()
    }
    

    瞧,如果您想使用 tableView,您可以查看 my github project

    【讨论】:

    • 谢谢拉马尔,我会试试的。我的不是 UIButton,但我认为它应该仍然可以工作,因为它是一个像按钮一样轻按的标注。代码: func mapView(mapView: MKMapView, annotationView: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) { if let samplesAnnotation = annotationView.annotation as? SpecimenAnnotation { 然后是上面的“performSegue”。我只是有点不确定我到底在哪里添加了你建议的代码?
    • 我以为我看到你在使用UIButton 哦,哈哈,试试吧,让我知道它是怎么回事]
    • PS 感谢您对您的 github 项目的提示 - 肯定会很好地集成到我的项目中。
    猜你喜欢
    • 2021-11-17
    • 1970-01-01
    • 1970-01-01
    • 2018-04-09
    • 2017-10-12
    • 2021-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多