尝试在 UIVIew 上像 GIF 一样循环运行视频

问题描述 投票:0回答:1

这是我尝试在 UI 视图上播放视频的功能,

func playVideo(videoString: String) { ``    if let videoURL = URL(string: videoString) { ``        self.player = AVPlayer(url: videoURL) ``        self.player?.isMuted = true 
self.player?.actionAtItemEnd = .none

    let playerLayer = AVPlayerLayer(player: player)
    playerLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
    playerLayer.bounds = CGRect(origin: .zero, size: self.videoView.bounds.size)
    playerLayer.position = CGPoint(x: self.videoView.bounds.midX, y:         self.videoView.bounds.midY)

    self.videoView.layer.addSublayer(playerLayer)

    self.player?.play()

    NotificationCenter.default.addObserver(self, selector: #selector(playerItemDidReachEnd), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: self.player?.currentItem)
    }
}

@objc func playerItemDidReachEnd(notification: NSNotification) {
self.player?.seek(to: CMTime.zero)
self.player?.play()
}

AVPlayerLayer 没有覆盖整个 UI View,如何使其覆盖整个 View?

我试图在 UIView 上播放视频,但 AVPlayerLayer 没有覆盖整个视图。

swift uiview avplayer avkit
1个回答
0
投票

您需要评论这一行

playerLayer.position = CGPoint(x: self.videoView.bounds.midX, y:self.videoView.bounds.midY)

因为它使

playerLayer
的左上角位于
videoView

的中间
© www.soinside.com 2019 - 2024. All rights reserved.