[英]My video goes off the screen, but stays within it's UIView
我有一个长宽比为 16:9 的 UIView,并相应地设置了约束。 但是,视频保留在 UIView 内,但有一半会离开屏幕。 有人知道怎么修这个东西吗? 只是为了澄清我正在尝试在 UIView 中创建视频。
import Foundation
import UIKit
import AVKit
import AVFoundation
class WallPush: UIViewController {
@IBOutlet weak var videoView: UIView!
@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var textView: UILabel!
var player: AVPlayer?
override func viewDidLoad() {
super.viewDidLoad()
// Load video resource
if let videoUrl = Bundle.main.url(forResource: "WallPush", withExtension: "mp4") {
if #available(iOS 10.0, *) {
player?.playImmediately(atRate: 1.0)
} else {
// Fallback on earlier versions
}
// Init video
self.player = AVPlayer(url: videoUrl)
self.player?.isMuted = true
self.player?.actionAtItemEnd = .none
// Add player layer
let playerLayer = AVPlayerLayer(player: player)
playerLayer.videoGravity = AVLayerVideoGravity.resizeAspect
playerLayer.frame = videoView.bounds
// Add video layer
self.videoView.layer.addSublayer(playerLayer)
// Play video
self.player?.play()
// Observe end
NotificationCenter.default.addObserver(self, selector: #selector(playerItemDidReachEnd), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: self.player?.currentItem)
}
}
// MARK: - Loop video when ended.
@objc func playerItemDidReachEnd(notification: NSNotification) {
self.player?.seek(to: CMTime.zero)
self.player?.play()
}
}
你需要把线放在里面
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
playerLayer.frame = videoView.bounds
}
也在viewDidLoad
集内
self.videoView.clipsToBounds = true
并声明
var playerLayer:AVPlayerLayer!
然后
playerLayer = AVPlayerLayer(player: player)
playerLayer.videoGravity = AVLayerVideoGravity.resizeAspect
class WallPush: UIViewController {
@IBOutlet weak var videoView: UIView!
@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var textView: UILabel!
var player: AVPlayer?
var playerLayer:AVPlayerLayer!
override func viewDidLoad() {
super.viewDidLoad()
// Load video resource
if let videoUrl = Bundle.main.url(forResource: "WallPush", withExtension: "mp4") {
print("File exists")
if #available(iOS 10.0, *) {
player?.playImmediately(atRate: 1.0)
} else {
// Fallback on earlier versions
}
// Init video
self.player = AVPlayer(url: videoUrl)
self.player?.isMuted = true
self.player?.actionAtItemEnd = .none
videoView.clipsToBounds = true
// Add player layer
playerLayer = AVPlayerLayer(player: player)
playerLayer.videoGravity = AVLayerVideoGravity.resizeAspect
playerLayer.frame = videoView.bounds // you can comment it
// Add video layer
self.videoView.layer.addSublayer(playerLayer)
// Play video
self.player?.play()
// Observe end
NotificationCenter.default.addObserver(self, selector: #selector(playerItemDidReachEnd), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: self.player?.currentItem)
}
else {
print("File not exists")
}
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
playerLayer.frame = videoView.bounds
}
// MARK: - Loop video when ended.
@objc func playerItemDidReachEnd(notification: NSNotification) {
self.player?.seek(to: CMTime.zero)
self.player?.play()
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.