AVFoundation自定义视频播放器不显示视频吗?

时间:2018-12-13 03:27:53

标签: swift avfoundation avplayer

我正在构建自定义视频播放器。要创建它,我使用播放按钮并查看。我想在播放器中观看的视频来自iPhone的照片。播放器的问题是我有一个视频的有效URL,但视图只是不显示该URL,实际上这是它的代码:

@IBAction func playVideo(_ sender: Any) {
    print("play video")

    let videoURL: NSURL = NSURL(string: uurl.url!)!
    let player = AVPlayer(url: videoURL as URL)
    playerLayerr = AVPlayerLayer(player: player)
    playerLayerr.frame = vlogView.bounds
    self.vlogView!.layer.addSublayer(playerLayerr)
    player.play()

}

一个有趣的事实是,播放器找到了视频,因为我可以听到它,但没有显示。

1 个答案:

答案 0 :(得分:0)

您可以尝试以下代码,它对我有用吗?

@IBOutlet weak var vlogView: UIView!

         var avPlayer = AVPlayer()
         var avPlayerLayer: AVPlayerLayer!

       @IBAction func playVideo(_ sender: Any) {
    let videoURL =  URL(string: "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")
        avPlayerLayer = AVPlayerLayer(player: avPlayer)
        self.avPlayerLayer.frame = self.vlogView.frame
        avPlayerLayer.videoGravity = .resizeAspectFill
        vlogView.layer.insertSublayer(avPlayerLayer, at: 0)
        vlogView.layoutIfNeeded()
        let playerItem = AVPlayerItem(url: videoURL!)
        avPlayer.replaceCurrentItem(with: playerItem)
        avPlayer.play()
        }