首页 > 解决方案 > 旋转设备时如何平滑调整视频层大小 | 斯威夫特 5

问题描述

目前我有以下功能用于播放和调整视频层的大小 - 旋转设备时 - 帧的大小不能平滑地调整到新位置。

func playVideo() {
            guard let path = Bundle.main.path(forResource: "video", ofType: "mp4") else {
                return
            }
            
            let asset: AVAsset = AVAsset(url: URL(fileURLWithPath: path))
            let playerItem = AVPlayerItem(asset: asset)
            let queuePlayer = AVQueuePlayer(playerItem: playerItem)

            self.playerLooper = AVPlayerLooper(player: queuePlayer, templateItem: playerItem)
            let playerLayer = AVPlayerLayer(player: queuePlayer)
            playerLayer.frame = self.view.bounds
            playerLayer.videoGravity = .resizeAspectFill
            self.videoLayer.layer.addSublayer(playerLayer)
            queuePlayer.play()
            
            videoLayer.bringSubviewToFront(img)
            videoLayer.bringSubviewToFront(signIn)
            videoLayer.bringSubviewToFront(stack)
            
        }

以下是我目前正在尝试调整的内容 - 可行但不流畅,视频停止播放几秒钟:

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
   super.viewWillTransition(to: size, with: coordinator)
    coordinator.animate(alongsideTransition: { (context) in
    }) { (context) in

        self.playVideo()
    }
}

标签: swift

解决方案


推荐阅读