Implementing video view in the Storyboard

前端 未结 4 2009
别跟我提以往
别跟我提以往 2021-02-06 13:49

I want to build simple video app thats views a video form youtube links thats users add. I didn\'t find \"VideoView\" I mean If image view is for image what is UIView for video

4条回答
  •  南旧
    南旧 (楼主)
    2021-02-06 14:25

    In Xcode 10 (with a bit less code):

    1. In the interface builder (Storyboard), add "AVKit Player View Controller"
    2. Create a View Controller that derives from AVPlayerViewController:

      import UIKit
      import AVKit
      
      class VideoPlayerViewController: AVPlayerViewController {
      
        override func viewDidLoad() {
          super.viewDidLoad()
      
          let videoURL = Bundle.main.url(forResource: "video", withExtension: "mp4")!
          self.player = AVPlayer(url: videoURL)
        }
      
        override func viewDidAppear(_ animated: Bool) {
          self.player?.play()
        }
      }
      
    3. Don't forget to connect this custom class in the storyboard's identity inspector :)

    Note: this example uses video URL pointing to a bundle's resource.

提交回复
热议问题