how to play a video in UIView swift?

后端 未结 3 1879
我寻月下人不归
我寻月下人不归 2021-02-03 10:14

I have a video which I want to download from a server and stream it in a fixed view. I\'ve set a UIView in my storyboard with fixed constraints, and here is what I\'ve done in c

相关标签:
3条回答
  • 2021-02-03 10:35

    the only way to add a video to a UIView with fixed constraints in storyboard, was this :

    let url = URL(string:myURL)
    
    player = AVPlayer(url: url!)
    
    avpController.player = player
    
    avpController.view.frame.size.height = videoView.frame.size.height
    
    avpController.view.frame.size.width = videoView.frame.size.width
    
    self.videoView.addSubview(avpController.view)
    

    I hope other can use this! :)

    0 讨论(0)
  • 2021-02-03 10:42

    If you want to use AVPlayerViewController:

    let videoURL = URL(string: "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")
    let player = AVPlayer(url: videoURL!)
    let playerViewController = AVPlayerViewController()
    playerViewController.player = player
    self.present(playerViewController, animated: true) {
        playerViewController.player!.play()
    }
    

    For AVPlayer:

    let videoURL = URL(string: "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")
    let player = AVPlayer(url: videoURL!)
    let playerLayer = AVPlayerLayer(player: player)
    playerLayer.frame = self.view.bounds
    self.view.layer.addSublayer(playerLayer)
    player.play()
    
    0 讨论(0)
  • 2021-02-03 10:44

    You can try to use AVPlayerLayer in a AVPlayer.

          let player = AVPlayer(url: video) // your video url
          let playerLayer = AVPlayerLayer(player: player)
          playerLayer.frame = videoView.bounds
          videoView.layer.addSublayer(playerLayer)
          player.play()
    
    0 讨论(0)
提交回复
热议问题