我想在ViewController中播放视频(而不是在单独的ViewController中)。
换句话说,我想在viewController的矩形View中播放视频。就像instagram帖子一样。但是我没有在对象库中找到任何视图……!

最佳答案

您可以尝试这样的事情

import AVKit

import 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功能满足您的需求

10-07 19:53