我想在我的 ViewController 中播放视频(而不是在单独的 ViewController 中)。 换句话说,我想在我的 viewController 中的矩形 View 中播放我的视频。就像 Instagram 帖子一样。但是我没有在对象库中找到任何 View ...!
最佳答案
你可以试试这样的东西
导入 AVKit
导入 AVFoundation
class VideoPlayerViewController: UIViewController {
@IBOutlet weak var videoPreviewLayer: UIView!
private var player: AVPlayer!
private var playerViewController = AVPlayerViewController()
override func viewDidLoad() {
super.viewDidLoad()
playVideoInView()
}
func playVideoInView() {
guard let path = NSBundle.mainBundle().pathForResource("videoName", ofType: "mp4") else {
return
}
let url = NSURL.fileURLWithPath(path)
player = AVPlayer(url: url)
playerViewController = AVPlayerViewController()
playerViewController.player = player
playerViewController.view.frame = videoPreviewLayer.frame
self.addChildViewController(playerViewController)
self.view.addSubview(playerViewController.view)
}
playVideoInView
函数有你需要的
关于ios - xcode:如何在 ViewController 中播放视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53352028/