在UIView中自动播放本地视频

时间:2017-08-24 12:16:29

标签: ios swift video

我想在UIView中播放本地视频 ,没有控件 我已将框架AVFoundation和AVKit导入到我的代码中。我还为UIView上课,但是,我不确定如何在我的UIView中播放没有控件的视频。我已经找到了一种从互联网播放视频的方法,这不是我需要的。

1 个答案:

答案 0 :(得分:1)

在swift 3.0中,试试这段代码。

//for Playing Video

@IBAction func btnvideoPlayClicked(_ sender:UIButton){

     self.videoPlay()
}

func videoPlay()
{
    let playerController = AVPlayerViewController()
    playerController.delegate = self

    let bundle = Bundle.main
    let moviePath: String? = bundle.path(forResource: "SoSorry", ofType: "mp4")
    let movieURL = URL(fileURLWithPath: moviePath!)

    let player = AVPlayer(url: movieURL)
    playerController.player = player
    self.addChildViewController(playerController)
    self.view.addSubview(playerController.view)
    playerController.view.frame = self.view.frame

    player.play()

}